/* Filename: readint.cpp Author: Br. David Carlson Date: February 1, 1998 Last Modified: February 21, 2013 This program reads a binary file, called int.dat, that contains all integers and displays them on the screen. See the makeint program to create such a file of 100 ints, although this readint program can handle a file of any number of ints. Tested with: Microsoft Visual C++ 6.0 Microsoft Visual C++ .NET g++ under Linux */ #include #include // needed for files #include // needed for setw using namespace std; void DisplayFile(fstream & InFile); int main(void) { fstream InFile; InFile.open("int.dat", ios::in | ios::binary); if (InFile.fail()) { cerr << "Could not open file int.dat" << endl; exit(1); } DisplayFile(InFile); InFile.close(); return 0; } /* Given: InFile A binary file stream of ints already opened for input. Task: To read and display on screen the ints from InFile. Return: InFile The modified file stream. */ void DisplayFile(fstream & InFile) { int Num, IntSize; IntSize = sizeof(Num); InFile.read(reinterpret_cast (&Num), IntSize); while (! InFile.fail()) { cout << setw(8) << Num; InFile.read(reinterpret_cast (&Num), IntSize); } cout << endl; }