0

I'm trying to use example from:

https://stackoverflow.com/a/6832677/1816083 but i have:

invalid conversion from `unsigned char*' to `char*'
initializing argument 1 of `std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::read(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]' 
invalid conversion from `void*' to `size_t'

in line:

size_t bytes_read = myfile.read((unsigned char *) buffer, BUFFER_SIZE);
Community
  • 1
  • 1
cerber
  • 1
  • 1
  • 1
  • 7
  • 3
    That could be part of the reason the answer you link to didn't get a single vote. ;) – NPE Mar 07 '13 at 08:37
  • @NPE There's one now. Well, -1 that is. – Mark Garcia Mar 07 '13 at 08:44
  • Do you just want the whole file into a linear array with one big fat read? or are you processing this one buffer-size at a time? – WhozCraig Mar 07 '13 at 08:54
  • I suggest to flag inappropriate answer, that leads to this question(http://stackoverflow.com/a/6832677/1816083), for removal, it confuses people – kassak Mar 07 '13 at 08:54

2 Answers2

3

Firstly, read() takes a char* rather than unsigned char*. Secondly, it does not return the number of characters read.

Instead, try:

myfile.read((char*)buffer, BUFFER_SIZE);
std::streamsize bytes_read = myfile.gcount();
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • invalid conversion from `unsigned char*' to `char*' maybe some include missing? i'm noob in cpp – cerber Mar 07 '13 at 08:41
  • @cerber remove word `unsigned` from buffer's definition – kassak Mar 07 '13 at 08:47
  • @kassak he needs it as an `unsigned char` array (see the title of the question). He should *add* a `(char *)` cast to the first param of the `.read()` call. – WhozCraig Mar 07 '13 at 08:50
  • @WhozCraig yep, missed this, looking only at question's body – kassak Mar 07 '13 at 08:51
  • #include #include #include #include using namespace std; int main() { const unsigned int BUFFER_SIZE = 1024; unsigned char buffer[BUFFER_SIZE]; //... ifstream myfile("myfile.bin", ios::binary); if (myfile) { size_t bytes_read = myfile.read(( char *) buffer, BUFFER_SIZE); } return 0; } invalid conversion from `void*' to `size_t' – cerber Mar 07 '13 at 08:57
  • @cerber that would be because the read operation does not return the size read. It returns a reference to the stream object. Use the `myfile.gcount()` as NPE's answer here shows to get the last-read size. – WhozCraig Mar 07 '13 at 08:58
  • myfile.read((char *)buffer, BUFFER_SIZE); std::streamsize bytes_read = myfile.gcount(); , compiles should be ok, thanks – cerber Mar 07 '13 at 09:02
1

IMHO the compiler's output is quite enought. It tells you, that you're trying to give unsigned char* to function, that waits char*. BTW, there is even a function name

std::basic_istream<_CharT, _Traits>::read(_CharT*, std::streamsize)
[with _CharT = char ...

If you need unsigned chars buffer[ ... ] then cast it to char*

unsigned char buffer[ BUFFER_SIZE ];
ifstream myfile("myfile.bin", ios::binary);
if (myfile)
{
    myfile.read((char*) buffer, BUFFER_SIZE);
    //          ^^^^^^^
    size_t bytes_read = myfile.gcount();
}
borisbn
  • 4,988
  • 25
  • 42