1: //textfile_io.cpp 2: //Illustrates C++ input from a file and output to a file. 4: #include <iostream> 5: #include <fstream> 6: using namespace std; 8: int main() 9: { 10: cout << "\nThis program reads data from a textfile called " 11: "\"in_data\", and writes it out\nto another textfile called " 12: "\"out_data\". The file in_data must be present.\n"; 13: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 15: ifstream inFile; //Declare a file variable for input 16: ofstream outFile; //Declare a file variable for output 17: inFile.open("in_data"); //Connect "inFile" to a physical file 18: outFile.open("out_data"); //Connect "outFile" to a physical file 20: int i1, i2, i3, i4, i5, i6; 21: char c1, c2, c3, c4, c5, c6; 22: double r1, r2, r3, r4, r5, r6; 24: inFile >> i1 >> i2 >> i3; 25: inFile >> c1 >> c2 >> c3; 26: inFile >> r1 >> r2 >> r3; 27: inFile >> i4 >> i5 >> i6; 28: inFile.get(c4); 29: inFile.get(c5); 30: inFile.get(c6); 31: inFile >> r4 >> r5 >> r6; 32: inFile.close(); //Close the input file 34: outFile << "\nHere are the eighteen values read in, with a " 35: "\"<<\" marker\nat the end of each output line:\n"; 36: outFile << "i1: " << i1 37: << "\t\t\ti2: " << i2 38: << "\t\t\ti3: " << i3 << "<<"; 39: outFile << "\nc1: " << c1 << "<<" 40: << "\nc2: " << c2 << "<<" 41: << "\nc3: " << c3 << "<<\n"; 42: outFile << "r1: " << r1 << "\t\tr2: " 43: << r2 << "\t\tr3: " 44: << r3 << "<<\n"; 45: outFile << "i4: " << i4 46: << "\t\t\ti5: " << i5 47: << "\t\t\ti6: " << i6 << "<<"; 48: outFile << "\nc4: " << c4 << "<<" 49: << "\nc5: " << c5 << "<<" 50: << "\nc6: " << c6 << "<<\n"; 51: outFile << "r4: " << r4 52: << "\t\t\tr5: " << r5 53: << "\t\t\tr6: " << r6 << "<<\n"; 54: outFile << endl; 55: outFile.close(); //Close the output file 57: cout << "\nIf all went well, the input file has been read, and " 58: "the output file has been\nwritten. However, you should, of " 59: "course, check to be sure.\n"; 60: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 61: }