gpt4 book ai didi

c - Linux 和 Windows 上结构的不同值

转载 作者:太空宇宙 更新时间:2023-11-04 11:16:34 25 4
gpt4 key购买 nike

我目前正在做一个 C 项目,我应该在 .bmp 图像中隐藏文本。

因此我打开一张图片,把文件头和信息头写成两种结构:

typedef struct _BitmapFileHeader_
{
uint16_t bfType_;
uint32_t bfSize_;
uint32_t bfReserved_;
uint32_t bfOffBits_;
}
__attribute__((packed))
BitmapFileHeader;

typedef struct _BitmapInfoHeader_
{
uint32_t biSize_;
int32_t biWidth_;
int32_t biHeight_;
uint16_t biPlanes_;
uint16_t biBitCount_;
uint32_t biCompression_;
uint32_t biSizeImage_;
int32_t biXPelsPerMeter_;
int32_t biYPelsPerMeter_;
uint32_t biClrUsed_;
uint32_t biClrImportant_;
}BitmapInfoHeader;

BitmapFileHeader* bitmap_headerdata = NULL;
BitmapInfoHeader* bitmap_infodata = NULL;

文件名是之前定义的

int readBitmapFile (char* filename, BitmapFileHeader** bitmap_headerdata,
BitmapInfoHeader** bitmap_infodata, unsigned char** bitmap_colordata)
{
FILE* bmp_file;
bmp_file = fopen(filename, "rb");

fseek(bmp_file, 0, SEEK_SET); // Set File Cursor to beginning
fread((*bitmap_headerdata), sizeof(**bitmap_headerdata), 1, bmp_file);
fseek(bmp_file, sizeof(**bitmap_headerdata), SEEK_SET);
fread((*bitmap_infodata), sizeof(**bitmap_infodata), 1, bmp_file);

int checkinfo = sizeof(**bitmap_infodata);
int checkheader = sizeof(**bitmap_headerdata);
printf("Size of Infodata: %d\nSize of Headerdata: %d\n", checkinfo, checkheader);

....
}

当我打开一个有效的位图(24 位,未压缩)并将值 bfType_、biBitCount 和 biCompression 与 Linux 上的 19778,24,0 进行比较时,它工作得很好,但是当我尝试在 Windows 上运行它时程序停止它将 biBitCount 与 24 进行比较。当我调试程序时,我注意到“bitmap_infodata”中的所有值都比它们应该在的位置高出一行(当我把它看成一张表时)。然后我比较了 Linux 和 Windows 上的 sizeof(**bitmap_headerdata),发现它在 Linux 上是 14,在 Windows 上是 16?

不应该是一样的吗?为什么结构 bitmap_headerdata 在两个操作系统上具有相同的值,但 bitmap_infodata 却不同?

伯恩哈德

最佳答案

问题在于结构在不同环境中的填充方式不同。

这个问题有 2 个解决方案。

1:逐字段读取表头。

2:删除结构填充。这样做的语法各不相同。一些编译器使用 #PRAGMA PACK。您正在使用 __attribute__((__packed__)) 这显然不适用于两个平台。

关于c - Linux 和 Windows 上结构的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20660897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com