// A simple example of a program using an end-of-file loop for input: // #include <fstream.h> // for file stream functions #include <iomanip.h> // for i/o manipulatorsint main() {
ifstream inStream; // declare file stream variables ofstream outStream;
int Int1, NumLines = 0; // declare other variables float Float1, Float2;
inStream.open("readme.dat"); // open files -- this associates DOS outStream.open("writeme.dat"); // file names with the stream variables
// Print header info to output file: outStream << "Here comes some output:" << endl << endl;
// Set up for floating point output: outStream.setf(ios::fixed, ios::floatfield); outStream.setf(ios::showpoint);
// Read first line of data from input file (priming read): inStream >> Float1 >> Float2 >> Int1;
// Process and read until there's no more data: while (inStream) {
NumLines++; // Count lines read.
outStream << " Float1 = " << setw(10) << setprecision(2) << Float1 << endl; outStream << " Float2 = " << setw(10) << setprecision(2) << Float2 << endl; outStream << " Int1 = " << setw(10) << Int1 << endl << endl;
// Read next line of input: inStream >> Float1 >> Float2 >> Int1; } // End of While loop
outStream << " Read " << setw(3) << NumLines; outStream << " lines of input data" << endl;
// Close files. inStream.close(); outStream.close();
return 0; }