/* Filename: readform.cpp Author: Br. David Carlson Date: February 1, 1998 Last modified: February 21, 2013 This program prompts the user for the name of a file to read. This must be a formatted text file as created by the makeform program. It assumes that each product name consists of just one word of up to 40 characters. The data from the file is displayed on the screen. Tested with: Microsoft Visual C++ 2008 g++ under Linux */ #include #include #include // needed for files using namespace std; const int MaxString = 120; typedef char StringType[MaxString]; void DisplayFile(fstream & InFile); int main(void) { StringType FileName; fstream InFile; cout << "Enter the name of the text file to be read: "; cin.getline(FileName, MaxString); InFile.open(FileName, ios::in); if (InFile.fail()) { cerr << "Could not open file " << FileName << endl; exit(1); } DisplayFile(InFile); InFile.close(); return 0; } /* Given: InFile A file stream already opened for input. Assumes: That InFile is associated with a text file of the type described at the top of this program. Task: To read the data from InFile and display it on the screen. Return: InFile The modified file stream. */ void DisplayFile(fstream & InFile) { StringType ProductName; float Price; int Count = 0; InFile >> ProductName; while (! InFile.fail()) { InFile >> Price; InFile.get(); // read the newline and discard it cout << setw(40) << ProductName << setw(12) << Price << endl; Count++; if (Count == 22) { Count = 0; cout << " Press ENTER to go on..."; cin.get(); } InFile >> ProductName; } if (Count != 0) { cout << " Press ENTER to go on..."; cin.get(); } }