/* Filename: readpart.cpp Author: Br. David Carlson Date: February 23, 1998 Revised: July 4, 1998 Revised: July 16, 2000 to use modern headers. 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. Warning: this simple program does not contain anything to pause the scrolling of the output on the screen. */ #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; }