home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch11 / perror.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  1KB  |  55 lines

  1. /*               perror.c
  2.  *
  3.  *   Synopsis  - Checks for the correct number of command line
  4.  *               parameters, opens the file in argv[1], reads
  5.  *               through the file and reports on any errors 
  6.  *               found.
  7.  *
  8.  *   Objective - To demonstrate the use of some of the error
  9.  *               handling functions.
  10.  */
  11.  
  12. /* Include Files */
  13. #include <stdio.h>                                     /* Note 1 */
  14. #include <stdlib.h>
  15.  
  16. /* Constant Definitions */
  17. #define BUF_SIZE  100
  18.  
  19. /* Function Prototypes */
  20. void processit( void );
  21. /*   PRECONDITION:  none
  22.  *   POSTCONDITION: processit() is a stub function
  23.  */
  24.  
  25. int main( int argc, char *argv[] )
  26. {
  27.      FILE  *fp;
  28.      char  buf[BUF_SIZE];
  29.  
  30.      if ( argc < 2 ) {
  31.           printf( "Usage: perror file\n" );
  32.           exit( 1 );
  33.      }
  34.      if (( fp = fopen( argv[1], "r" )) == NULL ) {
  35.           perror( "fopen error" );                     /* Note 2 */
  36.      }
  37.  
  38.      while ( fread( buf, sizeof( char ), BUF_SIZE, fp ) > 0 ) {
  39.           processit();
  40.      }
  41.      if ( ferror( fp )) {                              /* Note 3 */
  42.           perror( argv[1] );                           /* Note 4 */
  43.           clearerr( fp );                              /* Note 5 */
  44.      }
  45.  
  46.      /* Processing may continue */
  47.      return 0;
  48. }
  49.  
  50. /*******************************processit()*********************/
  51. void processit( void )
  52. {
  53.      printf( "File processing stub.\n" );
  54. }
  55.