//--------------------------------------------------------------------
// fileio2.cpp
//--------------------------------------------------------------------

// Reads a set of integer values from the file values.dat and outputs // their sum to the file results.dat.
#include <iostream.h> // For cin, cout #include <fstream.h> // For file input/output
void main() { ifstream inFile("values.dat"); // Open input file values.dat int num, // Number read from file sum = 0; // Sum of numbers read in
// Check that input file opened correctly. if ( !inFile ) cout << "values.dat could not be opened forinput" << endl; else { // Read in a set of numbers. Stop when the end of file is reached. while ( inFile.good() && inFile >> num ) sum += num;
// Close the input file. inFile.close();
// Open the output file results.dat. ofstream outFile("results.dat");
// Check that output file opened correctly. if ( !outFile ) cout << "sum.dat could not be opened for output" << endl; else { // Output the sum. outFile << "Sum = " << sum << endl;
// Close the output file stream. outFile.close(); } } }