这是我从bmp文件中读取的代码(使用windows.h)。它从BITMAPFILEHEADER读取正确的值,但从BITMAPINFOHEADER读取的所有值都是40(十六进制为28)。 怎么了?
ifstream file("d3.bmp",ios::binary);
BITMAPFILEHEADER fheader;
BITMAPINFOHEADER aImg;
ofstream fileOut("newfile5.txt",ios::app);
file.read((char*)&fheader.bfType, sizeof(WORD));
fileOut<<std::hex<<fheader.bfType<<"\n";
file.read((char*)&fheader.bfSize, sizeof(DWORD));
fileOut<<fheader.bfSize<<"\n";
file.read((char*)&fheader.bfReserved1, sizeof(WORD));
fileOut<<fheader.bfReserved1<<"\n";
file.read((char*)&fheader.bfReserved2, sizeof(WORD));
fileOut<<fheader.bfReserved2<<"\n";
file.read((char*)&fheader.bfOffBits, sizeof(DWORD));
fileOut<<fheader.bfOffBits<<"\n";
file.read((char*)&aImg.biSize,sizeof(DWORD));
fileOut<<aImg.biSize<<"\n";
file.read((char*)&aImg.biWidth, sizeof(LONG));
fileOut<<aImg.biSize<<"\n";
file.read((char*)&aImg.biHeight, sizeof(LONG));
fileOut<<aImg.biSize<<"\n";
file.read((char*)&aImg.biPlanes, sizeof(WORD));
fileOut<<aImg.biSize<<"\n";
file.read((char*)&aImg.biBitCount, sizeof(WORD));
fileOut<<aImg.biSize<<"\n";
file.read((char*)&aImg.biCompression, sizeof(DWORD));
fileOut<<aImg.biSize<<"\n";
file.read((char*)&aImg.biSizeImage, sizeof(DWORD));
fileOut<<aImg.biSize<<"\n";
file.read((char*)&aImg.biXPelsPerMeter, sizeof(LONG));
fileOut<<aImg.biSize<<"\n";
file.read((char*)&aImg.biYPelsPerMeter, sizeof(LONG));
fileOut<<aImg.biSize<<"\n";
file.read((char*)&aImg.biClrUsed, sizeof(DWORD));
fileOut<<aImg.biSize<<"\n";
file.read((char*)&aImg.biClrImportant, sizeof(DWORD));
fileOut<<aImg.biSize<<"\n";
答案 0 :(得分:1)
我敢打赌, read 的值是正确的,只是你总是输出一遍又一遍地输出
:file.read((char*)&aImg.biSize,sizeof(DWORD));
fileOut<<aImg.biSize<<"\n"; //ok, biSize
file.read((char*)&aImg.biWidth, sizeof(LONG));
fileOut<<aImg.biSize<<"\n"; //wait, biSize again?
file.read((char*)&aImg.biHeight, sizeof(LONG));
fileOut<<aImg.biSize<<"\n"; //well somehow I'm getting suspicious
file.read((char*)&aImg.biPlanes, sizeof(WORD));
fileOut<<aImg.biSize<<"\n"; //...
file.read((char*)&aImg.biBitCount, sizeof(WORD));
...