/* Filename: readtext.cpp Author: Br. David Carlson Date: February 23, 1998 Last Modified: November 23, 2001 This program reads a text file (named parts.txt) of parts data, displaying the info on the screen. The data for each part consists of an ID number, number in stock, price, and description, each on a new line of the file. See the related program, maketext.cpp, as a way to create a parts.txt file. Warning: this simple program does not contain anything to pause the scrolling of the output on the screen. Tested with: Microsoft Visual C++ 6.0 Microsoft Visual C++ .NET g++ under Linux */ #include // needed for files #include "parts.h" // needed for PartType const int MaxString = DescripMax; typedef char StringType[MaxString]; /* Given: fs A text file stream already opened for input. Task: To read parts data from fs, writing it onto the screen. Return: fs The modified file stream. */ void ReadFile(fstream & fs) { PartType Part; // Read the first line of the file: fs >> Part.ID; while (! fs.fail()) { // Read the rest of the data on this part: fs >> Part.NumInStock; fs >> Part.Price; fs.get(); // get the newline at the end of the price fs.getline(Part.Descrip, DescripMax); // Write data from Part onto the screen: cout << "ID: " << Part.ID << endl; cout << "Number in stock: " << Part.NumInStock << endl; cout << "Price: " << Part.Price << endl; cout << Part.Descrip << endl << endl; // Read the next ID from the file: fs >> Part.ID; } } int main(void) { fstream PartFile; PartFile.open("parts.txt", ios::in); if (PartFile.fail()) { cerr << "Could not open file parts.txt" << endl; exit(1); } ReadFile(PartFile); PartFile.close(); return 0; }