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

  1. //             file1.cpp
  2. //
  3. // Synopsis  - Opens an ifstream object (an input file)
  4. //             and an ofstream object(an output file)
  5. //             and copies the input file to the output file.
  6. //
  7. // Objective - To introduce file handling in C++.
  8. //
  9.  
  10. //Include Files
  11. #include <iostream.h>
  12. #include <fstream.h>                                 // Note 1
  13. #include <stdlib.h>
  14.  
  15. int main()
  16. {
  17.     ifstream infile("info");                         // Note 2
  18.  
  19.     if ( !infile ) {                                 // Note 3
  20.         cerr << "info couldn't be opened\n";
  21.         exit(1);
  22.     }
  23.     ofstream outfile;                                // Note 4
  24.     outfile.open("copy");                            // Note 5
  25.     if ( !outfile ) {                                // Note 6
  26.         cerr << "copy couldn't be opened\n";
  27.         exit(2);
  28.     }
  29.  
  30.     char c;
  31.     while( infile.get(c) )                           // Note 7
  32.         outfile.put(c);                              // Note 8
  33.  
  34.     infile.close();                                  // Note 9
  35.     outfile.close();                                 // Note 9
  36.     return 0;
  37. }
  38.