//--------------------------------------------------------- // FileIO_2.cpp // // This program demonstrates the basics of file input and // output using an array. It reads itself and then writes // itself to the display and to a backup file. // // D. Searls // Asbury College // Aug 2003 //--------------------------------------------------------- #include <iostream> #include <fstream> #include <string> using namespace std; const int MAXSIZE = 1000; //********************************************************* // M A I N D R I V E R //********************************************************* int main() { int n; // Number of lines of text string oneLine; // One line of text string text[MAXSIZE]; // The text ifstream inputFile; ofstream outputFile; inputFile.open("FileIO_2.cpp"); if (!inputFile.fail()) { // Read the contents of the input file n = 0; getline(inputFile, oneLine); while (!inputFile.fail() && n < MAXSIZE) { text[n] = oneLine; n = n + 1; getline(inputFile, oneLine); } if (!inputFile.fail()) { cout << "APPLICATION ERROR: "; cout << "There was too much data for this program to handle.\n\n"; } inputFile.close(); // Write the contents of the file to the display. for (int i = 0; i < n; i++) { cout << text[i] << endl; } // Write the contents of the file to a back-up file. outputFile.open("FileIO_2.bak"); if(!outputFile.fail()) { for (int i = 0; i < n; i++) { outputFile << text[i] << endl; } outputFile.close(); } else { cout << "Could not open the output file!\n\n"; } } else { cout << "Could not open the input file!\n\n"; } return 0; }
//--------------------------------------------------------- // FileIO_3.cpp // // This program demonstrates the basics of file input and // output using an array. It reads itself and then writes // itself to the display and to a backup file. // // D. Searls // Asbury College // Aug 2003 //--------------------------------------------------------- #include <iostream> #include <fstream> #include <string> using namespace std; const int MAXSIZE = 1000; //*********************************************** // readText // // Read text from the specified source. // // Preconditions: the source is open and has // not failed. // // In/Out parameter: source // // Out Parameters: text, n //*********************************************** void readText(istream& source, string text[], int& n) { string lineFromFile; n = 0; getline(source, lineFromFile); while (!source.fail() && n < MAXSIZE) { text[n] = lineFromFile; n++; getline(source, lineFromFile); } if (!source.fail()) { cout << "APPLICATION ERROR: "; cout << "There was too much data for this program to handle.\n\n"; } } //*********************************************** // writeText // // Write text to the specified destination. // // Precondition: the destination stream is open // and has not failed. // // In Parameters: text, n // // In/Out Parameter: destination //*********************************************** void writeText(ostream& destination, const string text[], int n) { for (int i = 0; i < n; i++) { destination << text[i] << endl; } } //********************************************************* // M A I N D R I V E R //********************************************************* int main() { int n; // Number of lines of text string text[MAXSIZE]; ifstream inputFile; ofstream outputFile; inputFile.open("FileIO_3.cpp"); if (!inputFile.fail()) { readText(inputFile, text, n); inputFile.close(); writeText(cout, text, n); outputFile.open("FileIO_3.bak"); if(!outputFile.fail()) { writeText(outputFile, text, n); outputFile.close(); } else { cout << "Could not open the output file!\n\n"; } } else { cout << "Could not open the input file!\n\n"; } return 0; }