[問題] 如果 feof()==1 還需要fclose嗎?(已補碼)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Linux, 但是這個問題應該不分平台吧?
我這邊在 0!=feof() 的時候 fclose 都會發生Segment fault
類似狀況到stackoverflow上去找解答,都是說:
1.因為重複fclose
2.fopen根本就沒有成功
但是我檢查過沒有上述兩點,所以懷疑是不是已經到檔案結尾時,就不用fclose?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.205.43
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1475552253.A.F59.html
→
10/04 11:39, , 1F
10/04 11:39, 1F
fclose 之前加上 fseek(fptrMD5List, 0, SEEK_SET); 還是沒用
→
10/04 11:44, , 2F
10/04 11:44, 2F
推
10/04 12:07, , 3F
10/04 12:07, 3F
推
10/04 12:49, , 4F
10/04 12:49, 4F
推
10/04 13:55, , 5F
10/04 13:55, 5F
→
10/04 14:12, , 6F
10/04 14:12, 6F
→
10/04 15:00, , 7F
10/04 15:00, 7F
→
10/04 17:40, , 8F
10/04 17:40, 8F
//打開檔案路徑與MD5清單
FILE* fptrMD5List = fopen(strMD5List,"r");
if(NULL == fptrMD5List)
{
printf("%s open failed !\n",(char*)fptrMD5List);
exit(1);
}
//逐檔驗證
while(NULL != fgets(strStream, 256, fptrMD5List))
{
//strsep會截走定位點之前的字串
strFilePath = strsep(&strStream,"\t");//截出檔案路徑
strFileMD5 = strsep(&strStream,"\n");//截出該檔案MD5
printf("Target File: %s\n",strFilePath);
//這邊都有印出資訊
//回圈內都沒動到 fptrMD5List
}
printf("fptrMD5List=%p\n",fptrMD5List);
if(NULL != fptrMD5List)
{
fclose(fptrMD5List); //Segmentation fault (core dumped)
}
※ 編輯: deo2000 (125.227.205.43), 10/04/2016 18:46:30
※ 編輯: deo2000 (125.227.205.43), 10/04/2016 18:52:49
→
10/04 18:53, , 9F
10/04 18:53, 9F
→
10/04 18:54, , 10F
10/04 18:54, 10F
※ 編輯: deo2000 (125.227.205.43), 10/04/2016 18:55:20
→
10/04 18:54, , 11F
10/04 18:54, 11F
→
10/04 18:56, , 12F
10/04 18:56, 12F
→
10/04 18:57, , 13F
10/04 18:57, 13F
推
10/04 19:03, , 14F
10/04 19:03, 14F
感謝, 做完 strsep 以後重新配置 strStream 就好了
printf前面加上這行
strStream = (char*)malloc(256);
※ 編輯: deo2000 (125.227.205.43), 10/04/2016 19:05:28
→
10/04 19:04, , 15F
10/04 19:04, 15F
→
10/04 19:07, , 16F
10/04 19:07, 16F
→
10/04 19:08, , 17F
10/04 19:08, 17F
→
10/04 19:17, , 18F
10/04 19:17, 18F
→
10/04 19:18, , 19F
10/04 19:18, 19F
→
10/04 19:19, , 20F
10/04 19:19, 20F
→
10/04 19:20, , 21F
10/04 19:20, 21F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章