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

  1.  /* Copying a file. */
  2.  
  3.  #include <stdio.h>
  4.  
  5.  int file_copy( char *oldname, char *newname );
  6.  
  7.  main()
  8.  {
  9.      char source[80], destination[80];
  10.  
  11.      /* Get the source and destination names. */
  12.  
  13.      printf("\nEnter source file: ");
  14.      gets(source);
  15.      printf("\nEnter destination file: ");
  16.      gets(destination);
  17.  
  18.      if ( file_copy( source, destination ) == 0 )
  19.          puts("Copy operation successful");
  20.      else
  21.          fprintf(stderr, "Error during copy operation");
  22.  }
  23.  
  24.  int file_copy( char *oldname, char *newname )
  25.  {
  26.      FILE *fold, *fnew;
  27.      int c;
  28.  
  29.      /* Open the source file for reading in binary mode. */
  30.  
  31.      if ( ( fold = fopen( oldname, "rb" ) ) == NULL )
  32.          return -1;
  33.  
  34.      /* Open the destination file for writing in binary mode. */
  35.  
  36.      if ( ( fnew = fopen( newname, "wb" ) ) == NULL  )
  37.      {
  38.          fclose ( fold );
  39.          return -1;
  40.      }
  41.  
  42.      /* Read one byte at a time from the source; if end of file */
  43.      /* has not been reached, write the byte to the */
  44.      /* destination. */
  45.  
  46.      while (1)
  47.      {
  48.          c = fgetc( fold );
  49.  
  50.          if ( !feof( fold ) )
  51.              fputc( c, fnew );
  52.          else
  53.              break;
  54.      }
  55.  
  56.      fclose ( fnew );
  57.      fclose ( fold );
  58.  
  59.      return 0;
  60.  }
  61.