Source of cppstr5.cpp


  1: // Filename: CPPSTR5.CPP
  2: // Purpose:  Reads and displays the lines from a textfile.
  3: //           Illustrates run-time input of filenames.

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

 10: #include "PAUSE.H"

 12: int main()
 13: {
 14:     cout << endl;
 15:     cout << "This program asks the user for the name "
 16:          << "of a textfile and then displays the "     << endl
 17:          << "lines from that file.  It then repeats "
 18:          << "the entire process one more time. "       << endl;
 19:     cout << endl;

 21:     ifstream inFile;
 22:     string line, fileName;

 24:     cout << "Enter name of first file to read: ";
 25:     cin >> fileName;  cin.ignore(80, '\n');  cout << endl;
 26:     inFile.open(fileName.c_str());
 27:     cout << "Here are the lines in the file " << fileName << ":\n";
 28:     getline(inFile, line);
 29:     while (inFile)
 30:     {
 31:         cout << line << endl;
 32:         getline(inFile, line);
 33:     }
 34:     inFile.close();  Pause(0);

 36:     cout << "Enter name of second file to read: ";
 37:     cin >> fileName;  cin.ignore(80, '\n');  cout << endl;
 38:     inFile.clear();
 39:     inFile.open(fileName.c_str());
 40:     cout << "Here are the lines in the file " << fileName << ":\n";
 41:     getline(inFile, line);
 42:     while (inFile)
 43:     {
 44:         cout << line << endl;
 45:         getline(inFile, line);
 46:     }
 47:     inFile.close();  Pause(0);

 49:     return 0;
 50: }