<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    神奇好望角 The Magical Cape of Good Hope

    庸人不必自擾,智者何需千慮?
    posts - 26, comments - 50, trackbacks - 0, articles - 11
      BlogJava :: 首頁 ::  :: 聯系 :: 聚合  :: 管理
    C 庫函數 feof(FILE*) 判斷文件末尾的問題 A Problem on Using C Library Function feof(FILE*) to Judge The End of A File
      我用 C 寫了一個程序讀取 32768 字節大小的文件,每次讀 16 個字節,應該是 2048 次讀完。但結果讀了 2049 次,并且最后兩次的數據相同,似乎重復讀取了最后 16 個字節。源代碼如下:     I wrote a program with C, which read a file of 32768 bytes, 16 bytes each time, and it should finish reading after 2048 times. But the reault was it read 2049 times, and the data of last two times are the same, which seemed the last 16 bytes were read twice. Here is the code:
    1. int loop = 0;
    2. while (!feof(file)) {
    3.     loop++;
    4.     fread(buffer, 16, 1, file);
    5.     ......
    6. }
    7. printf("%d\n", loop);    // 2049
      我看了一陣,發現導致這個錯誤的原因是 feof(FILE*) 判斷文件末尾的機制:文件指針在文件末尾的時候,除非再讀一次導致發生 I/O 錯誤,feof(FILE*) 依然返回 0。因此用 feof(FILE*) 作為判斷條件的 while 循環始終會多讀一次,而最后一次的讀取是失敗的,buffer 也就沒有改變,看起來就像是重復讀了一次。     I reviewed it for a whil and found the reason that produced this error is the mechanism feof(FILE*) used to judge the end of a file: When the file pointer is at the end of a file, feof(FILE*) still returns 0 unless reads one more time to course a I/O error. Therefore, a while loop using feof(FILE*) as the judgment condition always reads one more time, and the last time of reading will fail, so buffer stayed unchanged which looked like it repeated reading once.
      用下面的代碼就沒問題了:     Use the code below to solve this problem:
    1. int loop = 0;
    2. while (fread(buffer, 16, 1, file) == 1) {
    3.     loop++;
    4.     ......
    5. }
    6. printf("%d\n", loop); // 2048

    評論

    # 關于feof(FILE*)  回復  更多評論   

    2008-11-03 19:33 by friend
    其實可以在循環體內加上一句:
    if(feof(file))break;
    我估計就行了。

    # re: C 庫函數 feof(FILE*) 判斷文件末尾的問題[未登錄]  回復  更多評論   

    2014-10-31 17:52 by barry
    上面結論是對的,但下面的代碼也是有問題的,

    int loop = 0;
    while (fread(buffer, 16, 1, file) == 1) {
    loop++;
    ......
    }
    printf("%d\n", loop); // 2048

    應該改成:
    int loop = 0;
    while (fread(buffer, 16, 1, file) !=1) {
    loop++;
    ......
    }
    printf("%d\n", loop); // 2048

    if(feof(file)==EOF)
    ....
    if(ferror(file))
    ....

    # re: C 庫函數 feof(FILE*) 判斷文件末尾的問題[未登錄]  回復  更多評論   

    2014-10-31 17:59 by barry
    @barry

    sorry,

    while (fread(buffer, 16, 1, file) !=1
    應該還是
    while (fread(buffer, 16, 1, file) ==1
    只是最后結束循環次數未必一定是2048

    因為即使等于1,也可能是出錯的情況之一,這時候循環不會停止。
    如果要求出錯馬上退出,只能在循環中判斷。

    # re: C 庫函數 feof(FILE*) 判斷文件末尾的問題[未登錄]  回復  更多評論   

    2014-10-31 18:28 by barry
    尤其是塊數是N,而不是1的情況下,這樣寫更有意義
    while (fread(buffer, 16, N, file) ==N

    最后結束的時候,返回的塊數大多數情況下會小于N,這個時候,也有可能是因為出錯,沒有讀完N就出來了,或者到文件尾,剩下的塊小于N,也跑出來。

    后面要檢測不同結果,因為最終循環結束,未必是文件讀完了。

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 久久国产亚洲电影天堂| 国产91久久久久久久免费| 久久久高清日本道免费观看| 人人鲁免费播放视频人人香蕉| 亚洲成在人线aⅴ免费毛片 | 免费福利网站在线观看| 久久精品无码专区免费青青| 99久久人妻精品免费一区| 久久午夜夜伦鲁鲁片免费无码影视| 精品一区二区三区免费毛片爱| 99久在线国内在线播放免费观看| 1000部免费啪啪十八未年禁止观看 | 国产亚洲精品自在线观看| 久久亚洲2019中文字幕| 中文字幕亚洲综合久久男男| 亚洲综合日韩久久成人AV| 日本亚洲视频在线| 亚洲大片在线观看| 亚洲一区二区三区精品视频| 学生妹亚洲一区二区| 国产大陆亚洲精品国产| 一级毛片免费播放男男| a毛片在线看片免费| 在线看无码的免费网站| 毛片a级毛片免费观看免下载| 日本特黄特色免费大片| 亚洲国产精品一区二区第一页免 | 免费午夜爽爽爽WWW视频十八禁| 亚洲伊人久久成综合人影院| 亚洲国产精品一区第二页 | 国产亚洲精品xxx| 67pao强力打造67194在线午夜亚洲 | 日韩免费无砖专区2020狼| 久久伊人亚洲AV无码网站| 黑人精品videos亚洲人| 亚洲国产综合在线| 亚洲av无码有乱码在线观看| 一道本不卡免费视频| 国产精品免费看久久久| 成人免费淫片在线费观看| 亚洲色婷婷综合开心网|