Read and check if the file end has been reached : File End « File « C / ANSI-C






Read and check if the file end has been reached

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  FILE *fp;
  char ch;

  if((fp = fopen(argv[ 1 ], "r")) == NULL) {
    printf("Cannot open file.\n");
    exit(1);
  }

  while((ch=getc(fp))!=EOF) {
    printf("%c", ch);
  }

  fclose(fp);

  return 0;
}


           
       








Related examples in the same category

1.Reads files and displays them on the screen: use EOF
2.Copy a file: how to use feof
3.Check if End Of File has been reached: how to use feof