1: // Filename: CSTR5.CPP 2: // Purpose: Reads and displays the lines from a textfile. 3: // Illustrates run-time input of filenames. 4: // Try the values 81 and then 80 for "numChars" and 5: // explain the difference in behavior when the input 6: // file contains lines of exactly 80 characters. 8: #include <iostream> 9: #include <fstream> 10: #include <cstring> 11: using namespace std; 13: #include "PAUSE.H" 15: int main() 16: { 17: cout << endl; 18: cout << "This program asks the user for the name " 19: << "of a textfile and then displays the " << endl 20: << "lines from that file. It then repeats " 21: << "the entire process one more time. " << endl; 22: cout << endl; 24: ifstream inFile; 25: typedef char String80[81]; 26: String80 line, fileName; 28: int numChars; 29: cout << "Enter the maximum number of characters per line: "; 30: cin >> numChars; cin.ignore(80, '\n'); cout << endl; 32: cout << "Enter name of first file to read: "; 33: cin >> fileName; cin.ignore(80, '\n'); cout << endl; 34: inFile.open(fileName); 35: cout << "Here are the lines in the file " << fileName << ":\n"; 36: inFile.getline(line, numChars + 1); 37: while (inFile) 38: { 39: cout << line << endl; 40: inFile.getline(line, numChars + 1); 41: } 42: inFile.close(); Pause(0); cout << endl; 44: cout << "Enter name of second file to read: "; 45: cin >> fileName; cin.ignore(80, '\n'); cout << endl; 46: inFile.clear(); 47: inFile.open(fileName); 48: cout << "Here are the lines in the file " << fileName << ":\n"; 49: inFile.getline(line, numChars + 1); 50: while (inFile) 51: { 52: cout << line << endl; 53: inFile.getline(line, numChars + 1); 54: } 55: inFile.close(); Pause(0); cout << endl; 57: return 0; 58: }