判斷檔案結束函數(feof)

來源:互聯網
上載者:User

函數feof(fp//檔案指標)用於測試fp所指向的檔案內部位置指標是否指向檔案結束。如果是檔案結束,函數feof(fp)的值為1(真),如果不結束,函數feof(fp)的值為0(假),feof函數可判斷二進位檔案和文字檔的結束

例:

將一個文字檔資料複製到另一個檔案中(前提是a.txt是已存在的檔案)

#include <stdio.h>int main(){    FILE *fpin,*fpout;    char ch;    if((fpin = fopen("d:\\a.txt","rt")) == NULL)//開啟源檔案    {        printf("Cannot open a.txt\n");    }    if((fpout = fopen("d:\\b.txt","wt")) == NULL)//開啟目標檔案    {        printf("cannot open b.txt\n");    }    ch = fgetc(fpin); //讀取原檔案第一個字元    printf("直接輸出檔案a的內容\n");    while(!feof(fpin)) //判斷源檔案是否結束    {        putchar(ch);        fputc(ch,fpout);        ch = fgetc(fpin);    }    printf("\n");    fclose(fpin);    fclose(fpout);    printf("\n將a檔案複製到b檔案中後,輸出b檔案的內容\n");    if((fpout = fopen("d:\\b.txt","rt")) == NULL)    {        printf("Cannot open b.txt\n");    }    ch = fgetc(fpout);    while(!feof(fpout))    {        putchar(ch);        ch = fgetc(fpout);    }    fclose(fpout);    printf("\n");    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.