12321
V2EX  ›  C

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

  •  
  •   12321 · Dec 28, 2016 · 1855 views
    This topic created in 3449 days ago, the information mentioned may be changed or developed.

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

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5881 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 02:25 · PVG 10:25 · LAX 19:25 · JFK 22:25
    ♥ Do have faith in what you're doing.