Fread 跳过字符读入对象
我试图从其标题开始读取位图,但 fread 正在为我跳过字符。
我在我的标头中使用这个 typedef:
#include <windows.h> // Used for other
#include <cstdio>
typedef struct tagBITMAPHEADER{
WORD wFileType;
DWORD dwFileSize;
WORD dwReserved;
WORD dwReserved2;
DWORD dwBmpDataOffset;
DWORD dwBmpHeaderSize;
DWORD dwWidth;
DWORD dwHeight;
WORD wPlanes;
WORD wBitsPerPixel;
DWORD dwCompression;
DWORD dwBitmapDataSz;
DWORD dwHRes;
DWORD dwVRes;
DWORD dwColors;
DWORD dwImpColors;
} BITMAPHEADER, *PBITMAPHEADER;
在我的代码中,我只使用一个简单的 fopen 和 fread 与二进制。
#include "ImageLoader.h"
BITMAPHEADER pbhFileInfo;
FILE *fBitmap = fopen(FileName,"rb"); //Open file in read / binary
if (fBitmap) //File is now open
{ fread(&pbhFileInfo,sizeof(BITMAPFILEHEADER),1,fBitmap);
fclose(fBitmap);
}
虽然我的位图以“424DF25A0D”(十六进制)开头,但读入的前两个变量似乎跳过“F25A”
wFileType = 0x4d42 dwFileSize = 0x0000000d
知道会发生什么吗?
提前致谢。
I'm trying to read in a bitmap starting with its header, but fread is skipping characters for me.
I'm using this typedef in my header:
#include <windows.h> // Used for other
#include <cstdio>
typedef struct tagBITMAPHEADER{
WORD wFileType;
DWORD dwFileSize;
WORD dwReserved;
WORD dwReserved2;
DWORD dwBmpDataOffset;
DWORD dwBmpHeaderSize;
DWORD dwWidth;
DWORD dwHeight;
WORD wPlanes;
WORD wBitsPerPixel;
DWORD dwCompression;
DWORD dwBitmapDataSz;
DWORD dwHRes;
DWORD dwVRes;
DWORD dwColors;
DWORD dwImpColors;
} BITMAPHEADER, *PBITMAPHEADER;
And in my code, I just use a simple fopen and fread with binary.
#include "ImageLoader.h"
BITMAPHEADER pbhFileInfo;
FILE *fBitmap = fopen(FileName,"rb"); //Open file in read / binary
if (fBitmap) //File is now open
{ fread(&pbhFileInfo,sizeof(BITMAPFILEHEADER),1,fBitmap);
fclose(fBitmap);
}
Although my bitmap starts with '424DF25A0D' (hex), the first two variables read in seem to skip the 'F25A'
wFileType = 0x4d42
dwFileSize = 0x0000000d
Any idea what might be up?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,以这种方式使用结构是非常不明智的。是的,在这种情况下,您可以使用编译器特定的编译指示获得您想要的东西。如果您正在编写 Windows 设备驱动程序或其他已经非常特定于特定平台的东西,我会认为这是一个可接受的解决方案。
但这是加载标准格式的文件。它的代码可以在任何环境中运行。
就我个人而言,我会编写代码,将数据从字符数组中取出并手动将其放入结构中,而不是依赖编译器以正确的方式布置结构,这样 fread 就会神奇地把所有小数据位在正确的位置。
In my opinion it is highly unwise to be using a struct in this way. Yes, you can get what you want in this case with a compiler specific pragma. I would consider that an acceptable solution if you were writing a Windows device driver or something else that was already very specific to a particular platform.
But this is loading a file in a standard format. It's code that could run in any environment.
Personally, I would write code that lifted the data out of a character array and plopped it into the structure by hand instead of relying on the structure to be layed out in just the right way by the compiler such that fread will magically put all the little bits of data in the right places.
您的结构正在由编译器对齐。
您似乎正在使用 Visual C++。在
struct
定义之前添加此行:在定义之后添加此行
要了解更多信息,请参阅 为什么结构体的 sizeof 不等于每个成员的 sizeof 之和?
Your struct is being aligned by the compiler.
You seem to be using Visual C++. Add this line before your
struct
definition:And this line after the definition
To learn more, see Why isn’t sizeof for a struct equal to the sum of sizeof of each member?
您是否以二进制模式打开文件?如果没有,那么这就是您的问题,因为它将 424DF25A0D 中的“0D”转换为其他一些数据,因为它将其解释为回车/换行字符,而不仅仅是原始二进制数据。
Are you opening the file in binary mode? If not, then this is your problem as it will convert the '0D' in 424DF25A0D to some other data as it interprets it as a carriage return/line feed character, instead of just raw binary data.