1: //read_write.cpp 2: //Reads data from a file, and writes that data to another file. 3: //Then reads data from the keyboard and writes it to the screen. 4: //So what? So it uses the *same* read and write functions in *each* case. 5: //Among other things, this means the input data must have the same format 6: //in both situations: two lines of data, with two characters on the first 7: //line and three integers on the second. 10: #include <iostream> 11: #include <fstream> 12: using namespace std; 15: void DescribeProgram(); 16: void ReadData(istream& inFile, 17: char& firstInitial, char& lastInitial, 18: int& mark1, int& mark2, int& mark3); 19: void DisplayData(ostream& outFile, 20: char firstInitial, char lastInitial, 21: int mark1, int mark2, int mark3); 23: int main() 24: { 25: DescribeProgram(); 27: char firstI, secondI; 28: int m1, m2, m3; 31: ifstream inF; 32: inF.open("in_data"); 33: ReadData(inF, firstI, secondI, m1, m2, m3); 34: inF.close(); 36: ofstream outF; 37: outF.open("out_data"); 38: DisplayData(outF, firstI, secondI, m1, m2, m3); 39: outF.close(); 42: cout << "With luck, all data has now been transferred from " 43: "\"in_data\" to \"out_data\".\nTo verify this, check the " 44: "contents of \"out_data\" when the program finishes.\n"; 46: cout << "\nNow enter data from the keyboard:\n"; 47: ReadData(cin, firstI, secondI, m1, m2, m3); 48: cout << "\n\nHere is the data read in from the keyboard:\n"; 49: DisplayData(cout, firstI, secondI, m1, m2, m3); 50: cout << endl; 51: } 54: void DescribeProgram() 55: //Pre: The cursor is at the left margin. 56: //Post: The program description has been displayed, 57: // preceded and followed by at least one blank line. 58: { 59: cout << "\nThis program reads data from a file called \"in_data\", " 60: "and then writes it out\nto a file called \"out_data\". Next it " 61: "reads the same kind of data from the\nkeyboard and writes it " 62: "out to the screen. In both cases the input data must\nconsist " 63: "of two lines with two characters on the first line and three " 64: "integers\non the second. And be sure that the input data file " 65: "exists!\n\n"; 66: } 69: void ReadData(/* in */ istream& inFile, 70: /* out */ char& firstInitial, 71: /* out */ char& lastInitial, 72: /* out */ int& mark1, 73: /* out */ int& mark2, 74: /* out */ int& mark3) 75: //Pre: The file denoted by "inFile" exists, contains data in 76: // the proper format, and has been opened. 77: //Post: Data from "inFile" has been read into two char out-parameters 78: // and three int out-parameters. "inFile" is still open. 79: { 80: inFile >> firstInitial >> lastInitial; 81: inFile.ignore(80, '\n'); 82: inFile >> mark1 >> mark2 >> mark3; 83: inFile.ignore(80, '\n'); 84: } 87: void DisplayData(/* out */ ostream& outFile, 88: /* in */ char firstInitial, 89: /* in */ char lastInitial, 90: /* in */ int mark1, 91: /* in */ int mark2, 92: /* in */ int mark3) 93: //Pre: The file denoted by "outFile" exists, and has been opened. 94: // All other parameters have been initialized. 95: //Post: The values in the two char in-parameters and the three 96: // int in-parameters have been displayed on "outFile". 97: // "outFile" is still open. 98: { 99: outFile << "\nStudent's Initials: " 100: << firstInitial << lastInitial 101: << "\nStudent's Test Marks: " 102: << mark1 << " " << mark2 << " " << mark3; 103: outFile << endl << endl; 104: }