当前位置: C语言 -- 标准库 -- <stdio.h> -- feof

feof函数


概要:
#include <stdio.h>
int feof(FILE *stream);

描述:

该函数验证参数stream指向流的文件末尾指示符(end-of-file indicator)。

如果先前有操作尝试在文件末尾位置或者超过文件末尾位置读取数据,将会设置文件末尾指示符。文件末尾指示符设置后将一直存在,直至流关闭或者直至调用rewindfsetposfseekclearerr或者freopen函数。


参数:
FILE *stream

FILE类型指针,指向一个打开的流。


返回值:

当且仅当设置了流的文件末尾指示符时,函数返回非0值;否则函数返回0


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
/*函数feof范例*/

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

int main(void)
{
    FILE *pFile;
    int ch;

    /*打开文件。*/
    pFile = fopen("gch.txt", "r");
    if(pFile == NULL)
    {
        perror("Fail to open the file");
        exit(EXIT_FAILURE);
    }

    /*遍历文件。*/
    while((ch=getc(pFile)) != EOF)
    {
        putc(ch, stdout);
    }
    printf("\n");

    /*检查getc函数返回EOF原因。*/
    if(ch == EOF)
    {
        if(feof(pFile))
            puts("The stream is at end-of-file.");
    }

    /*关闭文件。*/
    fclose(pFile);

    return 0;
}


结果:

将输出gch.txt文件的内容;如果getc函数是由于到达文件末尾返回宏EOF,将输出The stream is at end-of-file.


相关内容:
EOF 表示文件末尾的宏。