To store and access arrays or class objects, binary files should be used. The first thing to remember about this type of file I/O is to open the file using the mode for binary access with the bit flag:
ios::binary
This flag can be used as in the following:
fstream theFile("thefile.txt", ios::in | ios::binary);
After the theFile is opened using a statement like the one above, it is possible to access the binary data for input (or output if ios::out was used).
To access data for input from a binary file once the file has been opened, the following fstream method could be used:
fstream::read(char* a_variable, long theSize);
A similar read( ) method is in the class ifstream.
For example, the member function read( ) could be used to read binary data from a file into the aClass object anObject as in the following:
aClass anObject; fstream aFile("sales.txt", ios::in | ios::binary); aFile.read((char *)(&anObject), sizeof(anObject));
Notice the need to type cast (char*) in the first argument. This statement converts from a pointer to an object of the class to a pointer to char upon which the function read( ) is based. For example see: inputsales.cpp
In addition to the Classical C++ way to type cast as in the example above, one of the newer type casting functions could be used as well. For example the code above could have been written using reinterpret_cast<const char*> as in the following:
aClass anObject; fstream aFile("sales.txt", ios::in | ios::binary); aFile.read(reinterpret_cast<const char*>(&anObject), sizeof(anObject));
To send arrays or objects to a binary file, the programmer could use the fstream method: write( ) whose construct is:
Fstream::write(char* aVariable, long theSize);
where again the write( ) function requires a type casting for either an object of a class or an array and where theSize is the size of the object or the array being written to the file.
A similar write( ) member function is also in the class ofstream.
For example the member function write( ) could be used as in the following:
aClass anObject; fstream aFile("sales.txt", ios::out | ios::binary); aFile.write((char*)(&anObject), sizeof(aClass));
Notice again the typecasting of the first argument from a pointer to an object of the class to a pointer to char. This typecasting could also be done with the newer typecasting: reinterpret_cast< >. For example see: outputsales.cpp.
To see how to read and write using binary files run outputsales.cpp and then run inputsales.cpp. After you have run these two programs, open up the data file: sales.txt in Notepad and observe the data stored in the file. Were you able to read the data stored in the file?
For an example of binary file I/O with arrays run the following two programs: outputSalesArray.cpp and then run: inputSalesArray.cpp. Open up the data file: arraySales.txt in Notepad and observe the data stored in the file. Were you able to read the data stored in the file? When the object is large and the number of elements in the array is also large, this may not be the best way to do file I/O.
The previous two programs did file I/O with arrays of objects. Another approach is to have a class whose objects are themselves arrays. For example compile and run: salesout.cpp and salesin.cpp. Next open up the binary data file: sales1.txt in Notepad and observe the data stored in the file.
The examples above only dealt with files with only one record. Next are examples of file I/O with several records of objects each of which is an array: salsimny.cpp and salsomny.cpp. Notice for output of the records that ios::app was used. Open up the data file: sales3.txt in Notepad and observe the data stored in the file. Notice that the input from the file in the program salsimny.cpp was controlled by (!file). For an example of using the function eof() see: salsimnyeof.cpp
Note: There seems to be a problem with Visual Studio when inputting or outputting strings using binary files. If the length of the string is in excess of 15 characters, then some of the data may be lost. Therefore you need to restrict the length of strings to 15 characters.