/* Filename: makepart.cpp Author: Br. David Carlson Date: February 23, 1998 Last Revised: February 21, 2013 This program creates a binary file of parts records. The data for each record consists of an ID number, number in stock, price, and description, and is read from the keyboard and written to this binary file, called parts.dat. Tested with: Microsoft Visual C++ 2008 g++ under Linux */ // iostream is indirectly included through parts.h #include // needed for files #include "parts.h" // needed for PartType const int MaxString = DescripMax; typedef char StringType[MaxString]; void WriteToFile(fstream & fs); int main(void) { fstream PartFile; PartFile.open("parts.dat", ios::out | ios::binary); if (PartFile.fail()) { cerr << "Could not create file parts.dat" << endl; exit(1); } WriteToFile(PartFile); PartFile.close(); return 0; } /* Given: fs A binary file stream already opened for output. Task: To read parts data from the user, writing each record of accumulated data to fs. Return: fs The modified file stream. */ void WriteToFile(fstream & fs) { int RecordSize, Number; PartType Part; RecordSize = sizeof(Part); cout << "Enter a part number (or 0 to exit): "; cin >> Number; while (Number != 0) { // Put data into the Part record: Part.ID = Number; cout << "Enter number in stock: "; cin >> Part.NumInStock; cout << "Enter price: "; cin >> Part.Price; cin.get(); // input newline at end of price cout << "Enter description: "; cin.getline(Part.Descrip, DescripMax); // Write the Part record to the binary file: fs.write(reinterpret_cast (&Part), RecordSize); cout << "Enter a part number (or 0 to exit): "; cin >> Number; } }