home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch10 / fopen2.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  719b  |  29 lines

  1. /*               fopen2.c
  2.  *
  3.  *   Synopsis  - Opens a file named info and closes it.
  4.  *
  5.  *   Objective - Demonstrates elementary error handling with
  6.  *               fopen() and fclose().
  7.  */
  8.  
  9. /* Include Files */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. int main( void )
  14. {
  15.      FILE *fp;
  16.                                                     /* Note 1 */
  17.      if (( fp = fopen( "info", "r" )) == NULL ) {
  18.           printf( "Input file could not be opened\n" );
  19.           exit( 1 );
  20.      }
  21.  
  22.      /* Other code to process the file might
  23.       * be placed here.
  24.       */
  25.  
  26.      if ( fclose( fp ) == EOF )                     /* Note 2 */
  27.      printf( "File couldn't be closed\n" );
  28.      return 0;
  29. }