home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c021 / 7.img / EXAMPLES.ZIP / DCOPY.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-04  |  1.3 KB  |  51 lines

  1. /* DCOPY.CPP -- Example from Chapter 5 of Getting Started */
  2.  
  3. /* DCOPY source-file destination-file                  *
  4.  * copies existing source-file to destination-file     *
  5.  * If latter exists, it is overwritten; if it does not *
  6.  * exist, DCOPY will create it if possible             *
  7.  */
  8.  
  9. #include <iostream.h>
  10. #include <process.h>    // for exit()
  11. #include <fstream.h>    // for ifstream, ofstream
  12.  
  13. main(int argc, char* argv[])  // access command-line arguments
  14. {
  15.    char ch;
  16.    if (argc != 3)      // test number of arguments
  17.    {
  18.       cerr << "USAGE: dcopy file1 file2\n";
  19.       exit(-1);
  20.    }
  21.  
  22.    ifstream source;    // declare input and output streams
  23.    ofstream dest;
  24.  
  25.    source.open(argv[1],ios::nocreate); // source file must be there
  26.    if (!source)
  27.    {
  28.       cerr << "Cannot open source file " << argv[1] <<
  29.            " for input\n";
  30.       exit(-1);
  31.    }
  32.    dest.open(argv[2]);   // dest file will be created if not found
  33.              // or cleared/overwritten if found
  34.    if (!dest)
  35.    {
  36.       cerr << "Cannot open destination file " << argv[2] <<
  37.           " for output\n";
  38.       exit(-1);
  39.    }
  40.  
  41.    while (dest && source.get(ch)) dest.put(ch);
  42.  
  43.    cout << "DCOPY completed\n";
  44.  
  45.    source.close();        // close both streams
  46.    dest.close();
  47. }
  48.  
  49.  
  50.  
  51.