C feof()
function is used to determine if the end of the file (stream) specified has been reached or not. This function keeps on searching the end of the file (EOF
) in your file program. This tutorial guides you on how to use the feof()
function in the C program.
Syntax:
int feof(FILE *stream)
Here is a program showing the use of feof()
function.
Example:
#include<stdio.h>
int main()
{
FILE *filee = NULL;
char buf[50];
filee = fopen("infor.txt","r");
if(filee)
{
while(!feof(filee))
{
fgets(buf, sizeof(buf), filee);
puts(buf);
}
fclose(filee);
}
return 0;
}
Output:
C feof()
function returns true in case the end of the file is reached; otherwise, it returns false
.
Explanation:
- It first tries to open a text file infor.txt in read-only mode.
- Then as the file gets opened successfully to read, it initiates the while loop.
- The iteration continues until all the statements/lines of your text file get to be read as well as displayed.
Lastly, you have to close the file.