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 / cp.c next >
C/C++ Source or Header  |  2005-06-16  |  1KB  |  41 lines

  1. /*               cp.c
  2.  *
  3.  *   Synopsis  - Makes a copy of a file. The names for the original
  4.  *               file and the copy are on the command line.
  5.  *
  6.  * Objective - To illustrate file handling.
  7.  */
  8.  
  9. /* Include Files */
  10. #include <stdio.h>                                  /* Note 1 */
  11. #include <stdlib.h>
  12.  
  13. int main( int argc, char *argv[] )
  14. {
  15.      FILE *fpin, *fpout;                            /* Note 2 */
  16.      int iochar;
  17.  
  18.      if ( argc != 3 ) {
  19.           printf( "Usage: cp oldfile newfile\n" );
  20.           exit( 1 );                                /* Note 3 */
  21.      }
  22.  
  23.      if (( fpin = fopen( argv[1], "r" )) == NULL ) {
  24.                                                     /* Note 4 */
  25.           printf( "Can't open input file.\n" );
  26.           exit( 1 );
  27.      }
  28.  
  29.      if (( fpout = fopen( argv[2], "w" )) == NULL ) {
  30.                                                     /* Note 5 */
  31.           printf( "Can't open output file.\n" );
  32.           exit( 1 );
  33.      }
  34.  
  35.      while (( iochar = getc( fpin )) != EOF )       /* Note 6 */
  36.           putc( iochar, fpout );
  37.  
  38.      fclose( fpin );
  39.      fclose( fpout );
  40.      return 0;
  41. }