摆烂大王- 2022-04-09 16:56 采纳率: 93.3%
浏览 10

文件读出出错,无法正常读出

问题遇到的现象和发生背景

一个商品库存系统 包括文件的读入读出,信息可以正确读入,但无法读出

问题相关代码,请勿粘贴截图

struct goods* load(goods* head) { //从文件中载入商品信息
FILE* fp;
fp = fopen("goodss.txt", "r");
if (fp == NULL) {
printf("不能打开这个文件\n");
exit(0);
}
goods* a = NULL, * cyclic = NULL;
while (!feof(fp)) { //从文件中读入商品
a = (goods*)malloc(sizeof(goods)); //动态内存分配
if (a == NULL) {
printf("Unable to allocate memory");
exit(0);
}
fscanf(fp, "%d%s%f", a->id, a->name, &(a->price));
a->next = NULL;
if (head == NULL) {
head = a;
cyclic = head;
}
else {
cyclic->next = a;
cyclic = cyclic->next;
}
}
fclose(fp);
return head;
}

运行结果及报错内容

img

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

2条回答 默认 最新

  • 辍之耕 2022-04-09 17:18
    关注

    你的fscanf(fp, "%d%s%f", a->id, a->name, &(a->price));是不是有问题

    评论

报告相同问题?

问题事件

  • 创建了问题 4月9日