EOF - C stdio.h

C examples for stdio.h:EOF

Type

constant

From

<cstdio>
<stdio.h>

Description

EOF is a macro of type int -1.

EOF is used to indicate that the End-of-File has been reached or to signal some other failure conditions.

EOF is also used as the value to represent an invalid character.

In C++, this macro is the value of char_traits<char>::eof().

Demo Code


#include <stdio.h>

int main ()//from   w  w w  . ja  v  a  2  s. com
{
  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