一个关于文件读取的问题
- C/C++ code
#include <stdio.h>#include <stdlib.h>#include <memory.h>#include <string.h>#define LENGTH 20#define SUB_LEN 512int main(void){ FILE *fp=NULL; char pch[20][512]; int i; FILE *fout=NULL; fp=fopen("rcl.txt","r"); if(fp==NULL) { printf("Can't open rcl.txt!\n"); return 0; } fout=fopen("result.txt","w"); if(fout==NULL) { printf("不能打开result.txt文件!\n"); return 0; } while(getc(fp)!=EOF) { fseek(fp,-1,SEEK_CUR); for(i=0;i<LENGTH && getc(fp)!=EOF; i++) { fseek(fp,-1,SEEK_CUR); if(fgets(pch[i],SUB_LEN,fp)) { printf("%d\n",i); } else { printf("%d\n",i); printf("读取失败!"); // return 0; } } } fclose(fp); fclose(fp); return 0;}
每次都会出现double free or corruption (!prev)
请问为什么?
[解决办法]
你连续调用了两次fclose(fp)。
[解决办法]
你在 程序末尾 写了两次 fclose(fp); 你的本意肯定是 fclose(fp);fclose(fout);