/* Filename: makeint.cpp Author: Br. David Carlson Date: February 1, 1998 Last Modified: February 21, 2013 This program creates a binary file, called int.dat, that contains 100 integers (specifically 1000, 990, 980, ..., 10, just as a convenient test). Tested with: Microsoft Visual C++ 2008 g++ under Linux */ #include #include // needed for files using namespace std; void WriteToFile(fstream & OutFile); int main(void) { fstream OutFile; OutFile.open("int.dat", ios::out | ios::binary); if (OutFile.fail()) { cerr << "Could not create file int.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 write 1000, 990, ..., 10 to OutFile. Return: OutFile The modified file stream. */ void WriteToFile(fstream & OutFile) { const int Max = 100; int k, Num, IntSize; IntSize = sizeof(Num); for (k = 0; k < Max; k++) { Num = 1000 - 10 * k; OutFile.write(reinterpret_cast (&Num), IntSize); } }