/* Filename: makeemp.cpp Author: Br. David Carlson Date: February 4, 1998 Last Revised: February 21, 2013 This program creates a binary file of employee records. The data for the records is read from the keyboard and written to this binary file, called emp.dat. The user presses CTRL z to end data entry. (Substitute CTRL d in Linux.) Tested with: Microsoft Visual C++ 2008 g++ under Linux */ // iostream is included indirectly via employee.h #include // needed for files #include "employee.h" // needed for EmployeeType void WriteToFile(fstream & OutFile); int main(void) { fstream OutFile; OutFile.open("emp.dat", ios::out | ios::binary); if (OutFile.fail()) { cerr << "Could not create file emp.dat" << endl; exit(1); } WriteToFile(OutFile); OutFile.close(); // very important to close an output file return 0; } /* Given: OutFile A binary file stream already opened for output. Task: To read records of employee data from the user, writing them to OutFile. Return: OutFile The modified file stream. */ void WriteToFile(fstream & OutFile) { int RecordSize, Result; EmployeeType Employee; RecordSize = sizeof(Employee); Result = ReadEmployee(Employee); while (Result == OKFlag) { OutFile.write(reinterpret_cast (&Employee), RecordSize); Result = ReadEmployee(Employee); } }