<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 :: 首頁(yè) ::  :: 聯(lián)系 :: 聚合  :: 管理
    C 庫(kù)函數(shù) feof(FILE*) 判斷文件末尾的問(wèn)題 A Problem on Using C Library Function feof(FILE*) to Judge The End of A File
      我用 C 寫(xiě)了一個(gè)程序讀取 32768 字節(jié)大小的文件,每次讀 16 個(gè)字節(jié),應(yīng)該是 2048 次讀完。但結(jié)果讀了 2049 次,并且最后兩次的數(shù)據(jù)相同,似乎重復(fù)讀取了最后 16 個(gè)字節(jié)。源代碼如下:     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
      我看了一陣,發(fā)現(xiàn)導(dǎo)致這個(gè)錯(cuò)誤的原因是 feof(FILE*) 判斷文件末尾的機(jī)制:文件指針在文件末尾的時(shí)候,除非再讀一次導(dǎo)致發(fā)生 I/O 錯(cuò)誤,feof(FILE*) 依然返回 0。因此用 feof(FILE*) 作為判斷條件的 while 循環(huán)始終會(huì)多讀一次,而最后一次的讀取是失敗的,buffer 也就沒(méi)有改變,看起來(lái)就像是重復(fù)讀了一次。     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.
      用下面的代碼就沒(méi)問(wèn)題了:     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

    評(píng)論

    # 關(guān)于feof(FILE*)  回復(fù)  更多評(píng)論   

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

    # re: C 庫(kù)函數(shù) feof(FILE*) 判斷文件末尾的問(wèn)題[未登錄](méi)  回復(fù)  更多評(píng)論   

    2014-10-31 17:52 by barry
    上面結(jié)論是對(duì)的,但下面的代碼也是有問(wèn)題的,

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

    應(yīng)該改成:
    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 庫(kù)函數(shù) feof(FILE*) 判斷文件末尾的問(wèn)題[未登錄](méi)  回復(fù)  更多評(píng)論   

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

    sorry,

    while (fread(buffer, 16, 1, file) !=1
    應(yīng)該還是
    while (fread(buffer, 16, 1, file) ==1
    只是最后結(jié)束循環(huán)次數(shù)未必一定是2048

    因?yàn)榧词沟扔?,也可能是出錯(cuò)的情況之一,這時(shí)候循環(huán)不會(huì)停止。
    如果要求出錯(cuò)馬上退出,只能在循環(huán)中判斷。

    # re: C 庫(kù)函數(shù) feof(FILE*) 判斷文件末尾的問(wèn)題[未登錄](méi)  回復(fù)  更多評(píng)論   

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

    最后結(jié)束的時(shí)候,返回的塊數(shù)大多數(shù)情況下會(huì)小于N,這個(gè)時(shí)候,也有可能是因?yàn)槌鲥e(cuò),沒(méi)有讀完N就出來(lái)了,或者到文件尾,剩下的塊小于N,也跑出來(lái)。

    后面要檢測(cè)不同結(jié)果,因?yàn)樽罱K循環(huán)結(jié)束,未必是文件讀完了。

    只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲国产人成精品| 国产亚洲精品国看不卡| 久久亚洲AV无码精品色午夜| 久久亚洲精品国产精品| 日韩免费在线视频| 1000部啪啪未满十八勿入免费| 亚洲福利在线观看| 99精品一区二区免费视频| 久久亚洲AV无码精品色午夜麻豆| 最近中文字幕免费2019| 亚洲AV成人无码天堂| 啦啦啦在线免费视频| 亚洲国产成人AV网站| 91精品免费不卡在线观看| 亚洲黄色在线观看视频| 97免费人妻无码视频| 国产亚洲精AA在线观看SEE| 亚洲妇女熟BBW| 国产一区二区三区在线免费| 色老头综合免费视频| 84pao国产成视频免费播放| 亚洲国产情侣一区二区三区| 无码高潮少妇毛多水多水免费 | 噜噜嘿在线视频免费观看| 国产亚洲综合视频| 国产福利电影一区二区三区,亚洲国模精品一区 | 精品一区二区三区免费毛片爱 | 免费视频成人国产精品网站| 4399好看日本在线电影免费| 亚洲中文字幕久久久一区| 一区二区三区亚洲视频| 无码人妻一区二区三区免费n鬼沢 无码人妻一区二区三区免费看 | 蜜臀98精品国产免费观看| 亚洲av专区无码观看精品天堂| 91在线品视觉盛宴免费| 青青草97国产精品免费观看| 久久亚洲国产视频| 浮力影院第一页小视频国产在线观看免费 | 久久精品成人免费国产片小草| 亚洲乱码日产一区三区| www视频在线观看免费|