Source of cstr6.cpp


  1: // Filename: CSTR6.CPP
  2: // Purpose:  Reads and displays the lines from a textfile, pausing
  3: //           whenever a line of 80 dollar signs ($) is encountered.

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

  9: #include "PAUSE.H"

 11: int main()
 12: {
 13:     cout << "\nThis program asks the user for the name "
 14:          << "of a textfile and then displays the "        << endl
 15:          << "lines from that file, pausing whenever "
 16:          << "it sees a line of 80 dollar signs ($).  "    << endl
 17:          << "Note that any such line of $ symbols "
 18:          << "is also actually displayed in the output. "  << endl
 19:          << "Since all lines from the files are read "
 20:          << "in before being displayed, there is a "      << endl
 21:          << "maximum number of lines (20) that the "
 22:          << "file can contain. "                          << endl << endl;

 24:     const int MAX_CHARS = 80;
 25:     const int MAX_LINES = 20;
 26:     typedef char LineType[MAX_CHARS+1];
 27:     typedef LineType FileContentsType[MAX_LINES];

 29:     ifstream inFile;
 30:     LineType fileName, line;
 31:     FileContentsType text;
 32:     int lineIndex = 0;

 34:     cout << "Enter name of file to display: ";
 35:     cin >> fileName;  cin.ignore(80, '\n');  cout << endl;

 37:     inFile.open(fileName);
 38:     inFile.getline(line, MAX_CHARS+2);
 39:     while (inFile)
 40:     {
 41:         strcpy(text[lineIndex], line);
 42:         inFile.getline(line, MAX_CHARS+2); // Note the "+2".
 43:         if (inFile) lineIndex++;
 44:     }
 45:     inFile.close();

 47:     int i;
 48:     char DOLLARS[MAX_CHARS+1] = "";
 49:     for (i = 0; i < MAX_CHARS; i++) strcat(DOLLARS, "$");
 50:     cout << "Here are the lines in the file " << fileName << ":\n";
 51:     for (i = 0; i <= lineIndex; i++)
 52:     {
 53:         cout << text[i] << endl;
 54:         if (strcmp(text[i], DOLLARS) == 0) Pause(0);
 55:     }
 56:     Pause(0);  cout << endl;

 58:     return 0;
 59: }