6.18.12 feof Function

Tests for end-of-file.

Attention: This function is not implemented by MPLAB XC8 for PIC MCUs. It is implemented by all other compilers, but MPLAB XC8 for AVR MCUs has limited support of data streams.

Include

<stdio.h>

Prototype

int feof(FILE * stream);

Argument

stream
stream to check for end-of-file

Return Value

Returns non-zero if stream end-of-file indicator is set; otherwise, returns zero.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

#include <stdio.h>

int main(void)
{
  FILE * myfile;
  int y = 0;

  if( (myfile = fopen( "afile.txt", "rb" )) == NULL )
    printf( "Cannot open file\n" );
  else
  {
    for (;;)
    {
      y = fgetc(myfile);
      if (feof(myfile))
        break;
      fputc(y, stdout);
    }
    fclose( myfile );
  }
}

Example Input

Contents of afile.txt (used as input):

This is a sentence.

Example Output

This is a sentence.