/* Filename: readpart.cpp Author: Br. David Carlson Date: February 23, 1998 Last Revised: November 23, 2001 This program reads a binary file (named parts.dat) of parts records, displaying the info on the screen. The data for each record consists of an ID number, number in stock, price, and description. See the related program makepart.cpp as a way to create a parts.dat 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 /* Given: fs A binary 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) { int RecordSize; PartType Part; RecordSize = sizeof(Part); // Read the first record from the file: fs.read(reinterpret_cast (&Part), RecordSize); while (! fs.fail()) { // 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 record from the file: fs.read(reinterpret_cast (&Part), RecordSize); } } int main(void) { fstream PartFile; PartFile.open("parts.dat", ios::in | ios::binary); if (PartFile.fail()) { cerr << "Could not open file parts.dat" << endl; exit(1); } ReadFile(PartFile); PartFile.close(); return 0; }