// Source: "Software Design ...", John A Robinson, Newnes, 2004, page 95. // Program to copy one file to another #include #include using namespace std; int main(int argc, char **argv) { if (argc != 3) { cerr << "Usage: copy source dest\n"; return(-1); } ifstream in(argv[1]); // Opens file given by argv[1] (first program line argument) // for reading // If working on a PC e.g. Borland C++, have to use // ifstream in(argv[1], ios::binary); if (!in) { cerr << "Can't open " << argv[1] << " for reading\n"; return(-1); } ofstream out(argv[2]); // Opens file given by argv[2] for writing. ofstream is the // mirror image of ifstream (just as cout is the mirror of cin) // If working on a PC e.g. Borland C++, have to use // ofstream out(argv[2], ios::binary); if (!out) { cerr <<"Can't open " << argv[2] << " for writing\n"; return(-1); } char c; while(in.get(c)) // Gets a character from the in file into c // in.get(c) returns 0 when the end of file is reached. out.put(c); // Puts the character in c into out file in.close(); out.close(); return(0); }