file_eof.c 633 B

123456789101112131415161718192021222324
  1. // file_eof.c --open a file and display it
  2. #include <stdio.h>
  3. #include <stdlib.h> // for exit()
  4. int main()
  5. {
  6. int ch;
  7. FILE * fp;
  8. char fname[50]; // to hold the file name
  9. printf("Enter the name of the file: ");
  10. scanf("%s", fname);
  11. fp = fopen(fname, "r"); // open file for reading
  12. if (fp == NULL) // attempt failed
  13. {
  14. printf("Failed to open file. Bye\n");
  15. exit(1); // quit program
  16. }
  17. // getc(fp) gets a character from the open file
  18. while ((ch = getc(fp)) != EOF)
  19. putchar(ch);
  20. fclose(fp); // close the file
  21. return 0;
  22. }