Source of dispfile.cpp


  1: // Filename: DISPFILE.CPP
  2: // Purpose:  Displays the contents of a textfile on the standard output.

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

  8: void DescribeProgram();
  9: void DisplayFile(ifstream& inFile);


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

 16:     ifstream inFile;
 17:     inFile.open("in_data");
 18:     cout << "========== Contents of file appear "
 19:          << "below this line ==========" << endl;
 20:     DisplayFile(inFile);
 21:     inFile.close();

 23:     return 0;
 24: }


 27: void DescribeProgram()
 28: // Pre:  none
 29: // Post: The program description has been displayed,
 30: //       preceded and followed by a blank line.
 31: {
 32:     cout << endl;
 33:     cout << "This program displays the contents "
 34:          << "of a textfile called \"in_data\"."          << endl;
 35:     cout << endl;
 36:     cout << "If no output (or bizarre output) appears "
 37:          << "below, the file is not yet available "      << endl;
 38:     cout << "on your system.  Check to be sure the "
 39:          << "file is present, and then try again. "      << endl;
 40:     cout << endl;
 41: }


 44: void DisplayFile(/* in */ ifstream& inFile)
 45: // Pre:  The file denoted by "inFile" exists, and has been opened.
 46: // Post: The data in "inFile" has been displayed.
 47: //       The file is still open.
 48: {
 49:     char ch;

 51:     inFile.get(ch);
 52:     while (!inFile.eof())
 53:     {
 54:         cout.put(ch);  // Another way to ouput a character
 55:         inFile.get(ch);
 56:     }
 57: }