當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C語言 feof()用法及代碼示例

C 中的 feof() 函數

原型:

    int feof(FILE* filename);

參數:

    FILE *filename

返回類型:整數(0 或 1)

函數的使用:

在 C 語言中,當我們使用與文件鏈接的流時,我們如何確定我們到達了文件的末尾。為了解決這個問題,我們必須將一個整數值與 EOF 值進行比較。通過 feof() 函數,我們可以確定文件是否發生了 EOF。這個函數的原型是int feof(FILE* filename);

當文件沒有結束時,它返回值零,否則返回 1。

C 中的 feof() 示例

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE* f;
    char str[100];

    //Check the existence of that file
    if ((f = fopen("includehelp.txt", "r")) == NULL) {
        printf("Cannot open the file...");
        //if not exist program is terminated
        exit(1);
    }

    printf("File content is--\n");
    //print the strings until EOF is encountered
    while (!feof(f)) {
        fgets(str, 100, f);
        //print the string
        printf("%s", str);
    }

    //close the opened file
    fclose(f);

    return 0;
}

輸出

feof example in c



相關用法


注:本文由純淨天空篩選整理自Souvik Saha大神的英文原創作品 feof() function in C language with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。