读取文件时为什么读完就崩溃了

2016-12-28 19:15:53 +08:00
 12321

(程序没有全写出来,只有读文件的函数) struct Information { int id; string name; float grade; struct Information* next; }*Ino_head = NULL; void addData(struct Information *d) { struct Information *p = new struct Information; struct Information *pnew = Ino_head; p->id = d->id; p->name = d->name; p->grade = d->grade; if (!Ino_head) Ino_head = p; else { pnew->next = p; pnew = pnew->next; pnew->next = NULL; } } void Load() { struct Information p; FILE *fp = fopen("data.txt", "rb"); if (!fp) return; while (fread(&p, 1, sizeof(struct Information), fp)) { addData(&p); } fclose(fp); } int main() { Load();

return 0;

}

加断点运行时,每次一到关闭文件后的大括号时就会崩溃。 报错是: 引发了异常: 读取访问权限冲突。

_Pnext 是 0xCE99A4 。

如有适用于此异常的处理程序,该程序便可安全地继续运行。 另外再问一个,在 addData 函数里分配的堆内存,怎么在文件全部读取完以后释放。

1396 次点击
所在节点    C
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/330822

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX