// Oldham, Jeffrey D. // 1999 Oct 18 // CS1320 // // Copy One File to Another File // input <- ask the user for the source file and destination file // output-> none // the destination file will be created and its contents will // be the same as the source file #include #include // has fstreams #include // has setw() #include // has exit() // Function Prototypes // Obtain a filename. void obtainFilename(char fileName[], const int fileNameLength, const char * description); int main() { // Obtain the names of the files. const int maxFileNameLength = 10000; char sourceFile[maxFileNameLength]; obtainFilename(sourceFile, maxFileNameLength, "source"); char destinationFile[maxFileNameLength]; obtainFilename(destinationFile, maxFileNameLength, "destination"); // Open the file streams. ifstream in; in.open(sourceFile); if (in.fail()) { cerr << "Failed to open " << sourceFile << ".\n"; cerr << "Bailing out!\n"; exit(1); } ofstream out; out.open(destinationFile); if (out.fail()) { cerr << "Failed to open " << destinationFile << ".\n"; cerr << "Bailing out!\n"; exit(1); } // Copy the file. char c; while (in.get(c)) out.put(c); // Close the file streams. in.close(); out.close(); return 0; } // precondition: fileName = array to hold the name of the file // fileNameLength = array's length // description = string describing the desired file // postcondition: the desired file is stored in fileName void obtainFilename(char fileName[], const int fileNameLength, const char description[]) { cout << "Please enter the filename of the " << description << " (max " << fileNameLength-1 << " characters) : "; cin >> setw(fileNameLength) >> fileName; return; }