我有一个我无法解决的问题...
我曾经fwrite将一个int值(以及来自 a 的其他值struct)写入我使用打开的文件中fopen(file, "wb");
这很好用,当我查看文件的内容时,我可以看到我“写入”的HEX值int(以及所有其他值)。
接下来,我尝试读取文件。因此,我使用打开文件fopen(file, "rb");并开始使用fread. 第一个int值被正确读取(例如 7)和它之后的所有其他值 - 属于struct我写的第一个值。
然后我尝试读取下一个值,struct当我尝试读取第一个int值时,我收到一个非常奇怪的数字(例如 1140850688),但它应该是 39。
所以问题是:为什么会这样?我该如何解决?
我的struct样子是这样的:
struct a {
int a1;
char* a2;
struct b* a3;
unsigned char a4;
int a5;
char* a6;
}
fread电话看起来像这样:fread(&(tmpA->a1), sizeof(int), 1, file);
编辑:
fwrite部分:
fwrite(&(tmpA->a1), sizeof(int), 1, file);
fwrite(tmpA->a2, sizeof(char), strlen(tmpA->a2), file);
fwrite(tmpA->a3, sizeof(struct b), 1, file);
fwrite(&(tmpA->a4), sizeof(unsigned char), 1, file);
fwrite(&(tmpA->a5), sizeof(int), 1, file);
fwrite(tmpA->a6, sizeof(char), strlen(tmpA->a6), file);
fread几乎是一样的。