feof() 函数在 stdio.h 头文件中定义。它用于测试给定参数流的 EOF(文件结束)指示器是否已设置。
int feof(FILE *stream); #where stream should be a file pointer
feof() 函数接受一个参数。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| stream | 要测试其文件结束指示器的流 | 必需 |
如果与流关联的 EOF 指示器已设置,则 feof() 函数返回一个非零值,否则返回零。
| 输入 | 返回值 |
|---|---|
| EOF 指示器已设置 | 非零 |
| EOF 指示器未设置 | 零 |
#include <stdio.h>
int main () {
FILE *pnt;
int chr;
pnt = fopen("testfile.txt","r");
if(pnt == NULL) {
perror("Error in opening file");
return(-1);
}
while(1) {
chr = fgetc(pnt);
if( feof(pnt) ) {
break ;
}
printf("%c", chr);
}
fclose(pnt);
return(0);
}
输出
/* The content of the file is the output */ C programming library functions
#include <stdio.h>
FILE *pnt = NULL;
char chr[50];
pnt = fopen("myfile.txt","r");
if(pnt)
{
while(!feof(pnt))
{
fgets(chr, sizeof(chr), pnt);
puts(chr);
}
fclose(pnt);
}
return 0;
输出
This is sample file It contains some text data ----------------------------- Process exited after 0.9847 seconds with return value zero