home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tyc / list19_4.c < prev    next >
C/C++ Source or Header  |  1993-10-16  |  500b  |  27 lines

  1.  /* Demonstration of error handling with perror() and errno. */
  2.  
  3.  #include <stdio.h>
  4.  #include <stdlib.h>
  5.  #include <errno.h>
  6.  
  7.  main()
  8.  {
  9.      FILE *fp;
  10.      char filename[80];
  11.  
  12.      printf("Enter filename: ");
  13.      gets(filename);
  14.  
  15.      if (( fp = fopen(filename, "r")) == NULL)
  16.      {
  17.          perror("You goofed!");
  18.          printf("errno = %d.", errno);
  19.          exit(1);
  20.      }
  21.      else
  22.      {
  23.          puts("File opened for reading.");
  24.          fclose(fp);
  25.      }
  26.  }
  27.