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:

  1. It first tries to open a text file infor.txt in read-only mode.
  2. Then as the file gets opened successfully to read, it initiates the while loop.
  3. 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.


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram