#include #include #include using namespace std; void copy(); void cat(); void display(); void outputFile( istream & in, ostream & out ); bool checkIfFileExists( string name ); int main( int argc, char * argv[] ) { // Display the menu bool flag = true; while( flag ) { cout << "1: Copy File\n" << "2: Concatenate File\n" << "3: Display File\n" << "4: Exit" << endl; cout << "Option: " << flush; int option; cin >> option; switch( option ) { case 1: copy(); break; case 2: cat(); break; case 3: display(); break; default: flag = false; } } return 0; } // Outputs a file to the specified ostream object. An ofstream is a child // of the ostream, hence it can be used in place of the out object void outputFile( istream & in, ostream & out ) { // Each stream must be open before calling this function string line; while( getline(in, line) ) { out << line << endl; } } void copy() { // Needs to ask for the names of the files to use string inFilename, outFilename; cout << "Enter the source filename: " << flush; cin >> inFilename; cout << "Enter the destination filename: " << flush; cin >> outFilename; // Open the source file and check to see that it opened properly ifstream in( inFilename.c_str() ); if( !in.is_open() ) { cout << "Error: cannot open file: " << inFilename << endl; return; } if( checkIfFileExists(outFilename) ) { // The file exists and the user does not wish to overwrite the file in.close(); cout << "Not opening file for writing" << endl; return; } ofstream out( outFilename.c_str() ); if( !out.is_open() ) { cout << "Error opening file: " << outFilename << endl; return; } // Call the function to output the infile to the outfile outputFile( in, out ); // Must close all files in.close(); out.close(); } void cat() { // Needs to ask for the names of the files to use string inFilename1, inFilename2, outFilename; cout << "Enter the source number 1 filename: " << flush; cin >> inFilename1; cout << "Enter the source number 2 filename: " << flush; cin >> inFilename2; cout << "Enter the destination filename: " << flush; cin >> outFilename; // Open the source file and check to see that it opened properly ifstream in1( inFilename1.c_str() ); if( !in1.is_open() ) { cout << "Error: cannot open file: " << inFilename1 << endl; return; } ifstream in2( inFilename2.c_str() ); if( !in2.is_open() ) { cout << "Error: cannot open file: " << inFilename2 << endl; return; } if( checkIfFileExists(outFilename) ) { // The file exists and the user does not wish to overwrite the file in1.close(); in2.close(); return; } ofstream out( outFilename.c_str() ); if( !out.is_open() ) { cout << "Error opening file: " << outFilename << endl; return; } // Call the function to output the first source file to the outfile outputFile( in1, out ); // Call the function to output the second source file to the outfile outputFile( in2, out ); // Remember to close all the files in1.close(); in2.close(); out.close(); } void display() { // Needs to ask for the name of the file to use string inFilename; cout << "Enter the source filename: " << flush; cin >> inFilename; // Open the source file and check to see that it opened properly ifstream in( inFilename.c_str() ); if( !in.is_open() ) { cout << "Error: cannot open file: " << inFilename << endl; return; } // Call the function to output the infile to the screen. // Notice the use of 'cout'. cout is an instance of the class 'istream' // so it can be used within this function outputFile( in, cout ); // Close the input file in.close(); } bool checkIfFileExists( string name ) { // Check for the existence of the output file ifstream tempOut( name.c_str() ); if( tempOut.is_open() ) { // The file exists already tempOut.close(); cout << "The file " << name << " already exists. Do you want to overwrite the file? (y/n)" << endl; string answer; cin >> answer; if( answer[0] == 'y' ) { return false; } else { return true; } } else { tempOut.close(); return false; } }