As far as I know, read() and write() are there so we can read and write bytes directly from or to a file, and I was taught that the equivalent of a byte in c++ is unsigned char, so why do they take char pointers as parameters?
Also, do take a look at this function from a "bmp file image reader" library I found:
bool BMPImage::readInfo()
{
//...
//read bmp and dib headers
unsigned char header[28] = {0};
_ifs->read((char*)header, 28);
_width = *(int*)&header[18]; //width is located in [18] and is 4 bytes size
_height = *(int*)&header[22]; //height is located in [22] and is 4 bytes size
_bpp = (unsigned char) *(short*)&header[28]; //bpp is located in [28] and is 2 bytes size
_channels = _bpp / 8; //set num channels manually
//...
Why does the _ifs->read() line work anyway? The cast from unsigned char to char forces loss of data, no?
Given that
charandunsigned charhave the same size, there should be no data loss when converting between them.Said that, have in mind that
fstreammis just an specialization ofstd::basic_fstreamfor chars:You can create your own type for unsigned char, like this: