-1

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);
4
  • 3
    That could be part of the reason the answer you link to didn't get a single vote. ;) Commented Mar 7, 2013 at 8:37
  • @NPE There's one now. Well, -1 that is. Commented Mar 7, 2013 at 8: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? Commented Mar 7, 2013 at 8:54
  • I suggest to flag inappropriate answer, that leads to this question(stackoverflow.com/a/6832677/1816083), for removal, it confuses people Commented Mar 7, 2013 at 8:54

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

7 Comments

invalid conversion from unsigned char*' to char*' maybe some include missing? i'm noob in cpp
@cerber remove word unsigned from buffer's definition
@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 yep, missed this, looking only at question's body
#include <fstream> #include <iostream> #include <vector> #include <string> 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'
|
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();
}

5 Comments

stackoverflow.com/a/6832677/1816083 , so this example is bad, how to do this properly?
@cerber And what do you always believe people?)
"so this example is bad" - yes. "how to do this properly?" - declare buffer as array of char's or cast it to char* instead of unsigned char*
but i need uchar array
Then cast it to char*. BTW, you choose that answer with negative vote, while there was another answers =)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.