feof - C stdio.h

C examples for stdio.h:feof

Type

function

From


<cstdio>
<stdio.h>

Prototype

int feof ( FILE * stream );

Description

Checks whether the end-of-File indicator is set. Returning a non-zero if it is. This indicator is cleared by a call to

  • clearerr,
  • rewind,
  • fseek,
  • fsetpos or
  • freopen.

Parameters

ParameterDescription
stream Pointer to a FILE object that identifies the stream.

Return Value

A non-zero value is returned if the end-of-file indicator is set. Otherwise, zero is returned.

Example

The following code opens the file, and counts the number of characters that it contains.

The program checks whether the end-of-file was reached, and if so, prints the total number of bytes read.

Demo Code

#include <stdio.h>

int main (){/*from w  w  w. j  a  va 2s.  co m*/
  FILE * pFile;
  int n = 0;
  pFile = fopen ("main.cpp","rb");
  if (pFile==NULL) {
     perror ("Error opening file");
     return -1;
  }

  while (fgetc(pFile) != EOF) {
      ++n;
  }
  if (feof(pFile)) {
      puts ("End-of-File reached.");
      printf ("Total number of bytes read: %d\n", n);
  }
  else
     puts ("End-of-File was not reached.");
  fclose (pFile);

  return 0;
}

Related Tutorials