V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
12321
V2EX  ›  C

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

  •  
  •   12321 · 2016-12-28 19:15:53 +08:00 · 1394 次点击
    这是一个创建于 2691 天前的主题,其中的信息可能已经有所发展或是发生改变。

    (程序没有全写出来,只有读文件的函数) 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 函数里分配的堆内存,怎么在文件全部读取完以后释放。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2822 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 07:01 · PVG 15:01 · LAX 00:01 · JFK 03:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.