Easy Tutorial
❮ C Function Strcpy C Examples Divide Concatenation Array ❯

C Library Function - feof()

C Standard Library - <stdio.h>

Description

The C library function int feof(FILE *stream) tests the end-of-file indicator for the given stream.

Declaration

Here is the declaration for the feof() function.

int feof(FILE *stream)

Parameters

Return Value

The function returns a non-zero value when the end-of-file indicator associated with the stream is set, otherwise it returns zero.

Example

The following example demonstrates the use of the feof() function.

#include <stdio.h>

int main ()
{
   FILE *fp;
   int c;

   fp = fopen("file.txt","r");
   if(fp == NULL) 
   {
      perror("Error opening file");
      return(-1);
   }
   while(1)
   {
      c = fgetc(fp);
      if( feof(fp) )
      { 
          break ;
      }
      printf("%c", c);
   }
   fclose(fp);
   return(0);
}

Assuming we have a text file file.txt with the following content, which will be used as input in our example program:

This is tutorialpro.org

Let's compile and run the above program, which will produce the following result:

This is tutorialpro.org

C Standard Library - <stdio.h>

❮ C Function Strcpy C Examples Divide Concatenation Array ❯