据我所知,read()
是否write()
可以直接从文件读取字节或向文件写入字节,并且我被告知byte
c++ 中的 a 等价物是unsigned char
,那么为什么它们将char
指针作为参数?
另外,请从我发现的“bmp 文件图像阅读器”库中查看此函数:
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
//...
为什么这_ifs->read()
条线仍然有效?从 unsigned char 转换为 char 强制数据丢失,不是吗?