When I write data to and from a buffer to save to a file I tend to use std::vector<unsigned char>
, and I treat those unsigned chars just as bytes to write anything into, so:
int sizeoffile = 16;
std::vector<unsigned char> buffer(sizeoffile);
std::ifstream inFile("somefile", std::ios::binary | std::ios::in);
inFile.read(buffer.data(), sizeoffile); // Argument of type unsigned char* is incompatible
// with parameter of type char*
The first argument of ifstream::read()
wants a char
pointer, but my vector buffer is unsigned char
. What sort of cast is suitable here to read the data into my buffer? It's essentially a char*
to unsigned char*
. I can do with reinterpret_cast or a C-style cast, but this makes me think I'm doing something wrong as these are not very often recommended at all. Have I made the wrong choice of data type (unsigned char) for my buffer?