The previous sections showed how to make a class serializable. Once you have a serializable class, you can serialize objects of that class to and from a CArchive object. This section shows how to get a CArchive object and how to serialize objects to and from the archive.
·To create a CArchive object:
CArchive objects are always associated with files. If you have a CFile object or a derived CFile object, you can get a CArchive object for that file by passing the CFile object to the constructor for CArchive, as shown in the following example:
CFile theFile;
theFile.Open( ..., CFile::modeWrite );
CArchive archive( &theFile, CArchive::store );
The second argument to the CArchive constructor is an enumerated value that specifies whether the archive will be used for loading (reading) or storing (writing) data. An archive can be for either storing or loading, but not for both. This constructor argument sets the load-store state for the archive. The Serialize function of an object checks this state by calling the IsStoring function for the archive object.
·To serialize an object:
Once you have a CArchive object, as shown in the previous section, you can serialize objects to and from it (depending on whether it was created for storing or loading) by using the same insertion and extraction operators that you used inside the Serialize member function described in an earlier procedure, “Overriding the Serialize Member Function,” on pages 281-282.
Assuming that CPerson is a class that has overridden the Serialize member function, you can serialize a CPerson object to an archive as follows:
CPerson* aPerson = new CPerson( "Smith" );
archive << aPerson;
Summary: Close the archive before you close the file.
To flush the buffers and close the connection between the archive and the file when you are done with the serialization operation, call the Close member function for the archive. Be sure to close the archive before you close the file, as shown in the following example:
archive.Close();
theFile.Close();
·To deserialize an object:
Deserializing an object involves reading the object back in from the disk file to which it was originally serialized. The following example shows how you can deserialize the object:
CPerson* aPerson2;
CFile theFile;
theFile.Open( ..., CFile::modeRead );
CArchive archive2( &theFile, CArchive::load );
archive2 >> aPerson2;
archive2.Close();
theFile.Close();
Deserialization will only work if the file position is in the same state as it was when the object was originally serialized. That means your program must keep track of the order in which objects were serialized and deserialize them in the same order.
The suggested way to avoid this bookkeeping problem is to place all your objects in a collection and then serialize the collection as a single object. Your data file can then be thought of as containing a single collection object that can be deserialized. When the collection object is deserialized, it will take care of deserializing all its constituent objects in the proper order.
Note:
Note that you do not have to do any explicit memory allocation for the objects that you are deserializing. The archive allocates the memory necessary to reconstruct the objects. You are responsible, however, for deleting the deserialized objects when you are done with them.
See the tutorial in this book (Chapters 1-6) for a complete example program that uses serialization to save and restore a name and phone-number data base as a single collection.