// // Program name: copyfile // Author: J. Oldham // Modified by: B. Massingill // // Purpose: copy one file to another file // // Input: // source and destination file names (from user) // Output: // no printed output; destination file will be created, and its // contents will be the same as the contents of 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); // // **** Main Program **** // int main() { // Obtain the names of the files. const int maxFileLength = 10000; char sourceFile[maxFileLength]; obtainFilename(sourceFile, maxFileLength, "source"); char destinationFile[maxFileLength]; obtainFilename(destinationFile, maxFileLength, "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; }