问题描述
我正在尝试将结构数组写入二进制文件并读回其内容。有人建议我使用带有 sizeof 的“fwrite”,并且它部分起作用了。这是我的代码:
structObject structArray[50];
structArray[0] = newObject // Not shown here but a new element is added to the main array from user input
structObject read[1]; //This array is for reading only
FILE* fp = fopen("storeStructs.bin","wb");
fwrite(structArray,sizeof(structObject),1,fp);
fflush(structArray);
fclose(fp);
fp = fopen("storeStructs.bin","rb");
fread(read,fp);
printf("\n\n The name is: %s\n\n",read[0].name);
//name is a char array
这适用于整数,但不适用于字符数组。
编辑:不知道我怎么没有意识到,但我使用的是 %d 而不是 %s。然而,这并没有解决问题,现在它只是不返回任何内容。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)