Source of display_file_data.cpp


  1: //display_file_data.cpp
  2: //Displays the data from a file, provided
  3: //the data in the file has a certain format.

  5: #include <iostream>
  6: #include <fstream>
  7: using namespace std;

  9: void DescribeProgram();
 10: void ReadAndDisplayFileData(ifstream& inFile);

 12: int main()
 13: {
 14:     DescribeProgram();

 16:     ifstream inFile;
 17:     inFile.open("in_data");
 18:     cout << "Here is the data from the file: " << endl;
 19:     ReadAndDisplayFileData(inFile);
 20:     inFile.close();
 21: }

 23: void DescribeProgram()
 24: //Pre:  The cursor is at the left margin.
 25: //Post: The program description has been displayed,
 26: //      preceded and followed by at least one blank line.
 27: {
 28:     cout << "\nThis program displays all the data from a file called "
 29:         "\"in_data\".\nIf no output (or bizarre output) appears below, "
 30:         "the file is not yet\navailable on your system. Study the "
 31:         "source code, create a file with\nthe appropriate format, and "
 32:         "then try again.\n\n";
 33: }

 35: void ReadAndDisplayFileData(/* in */ ifstream& inFile)
 36: //Pre:  The file denoted by "inFile" exists, contains data in
 37: //      the proper format, and has been opened.
 38: //Post: The data in "inFile" has been displayed.
 39: {
 40:     char firstInitial, lastInitial;
 41:     int mark1, mark2, mark3;

 43:     inFile >> firstInitial >> lastInitial;
 44:     inFile.ignore(80, '\n');
 45:     inFile >> mark1 >> mark2 >> mark3;
 46:     inFile.ignore(80, '\n');

 48:     cout << "\nStudent's Initials:   "
 49:          << firstInitial << lastInitial
 50:          << "\nStudent's Test Marks: "
 51:          << mark1 << "  " << mark2 << "  " << mark3;
 52:     cout << endl << endl;
 53: }