// Reads three numbers from the file specified by the user, sums the // numbers, and writes the sum to another file specified by the user. // mixes console I/O and file I/O // Walt Savitch // 03/14/00 #include #include #include #include using std::ifstream; using std::ofstream; using std::endl; using std::cin; using std::cout; using std::string; int main() { ifstream fin; ofstream fout; string inFileName; string outFileName; cout << "Enter the input file name: "; cin >> inFileName; cout << "Enter the output file name: "; cin >> outFileName; cout << "I will read numbers from the file " << inFileName << "and place the sum in the file " << outFileName << endl; fin.open(inFileName.c_str()); if (fin.fail()) { cout << "Input file opening failed.\n"; exit(1); } fout.open(outFileName.c_str()); if (fout.fail()) { cout << "Output file opening failed.\n"; exit(1); } int first, second, third; fin >> first >> second >> third; fout << "The sum of the first three numbers in" << inFileName << "is " << first + second + third << endl; fin.close( ); fout.close( ); cout << "End of Program.\n"; }