gpt4 book ai didi

c - 为什么我的结构中的 fread() 不起作用?

转载 作者:行者123 更新时间:2023-11-30 17:50:16 25 4
gpt4 key购买 nike

我有以下代码,可以从文件读取到结构并读取到数组中。当我尝试打印结构中的数据时,这不是我所期望的。该数组打印出预期的内容,即文件中的前两个字符。

typedef struct __attribute__((packed)){
uint8_t magic[2]; /* the magic number used to identify the BMP file:
0x42 0x4D (Hex code points for B and M).
The following entries are possible:
BM - Windows 3.1x, 95, NT, ... etc
BA - OS/2 Bitmap Array
CI - OS/2 Color Icon
CP - OS/2 Color Pointer
IC - OS/2 Icon
PT - OS/2 Pointer. */
} bmp_header_t;


bool
bmp_get_header_from_file(FILE *fp, bmpfile_t *bmp)
{
fseek(fp, 0L, SEEK_SET);
char magic[1];

fread(magic, 1, 2, fp);
printf("magic is: %c, %c\n", magic[0], magic[1]);

fread(&bmp->header, 1, 2, fp);
printf("magic is: %c, %c\n", bmp->header.magic[0], bmp->header.magic[1]);
}

最佳答案

当您执行第一个 fread 时,您会提前读取文件的位置,因此第二个 fread 将读取接下来的两个字节。您需要在第二次读取之前添加另一个 fseek

关于c - 为什么我的结构中的 fread() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17376319/

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