The process of saving objects to a file and restoring objects from a file is
called serialization. The Serialize() function load the data from or store data
in an archive object. All the data associated with a serializable object is
sequentially read from or written to a single disk file. It is not possible to
access individual data at random disk file addresses.
The disk file on which the data will be stored is represented by an object of
class CFile. The Serialize () function and the CFile object communicate using an
archive object of class CArchive. The CArchive::IsStoring() member function
tells us whether the archive is currently being used for storing to a file or
loading from a file. The CArchive class has overloaded insertion operators (<<)
and extraction operators (>>) for the data types: byte, word, long, DWORD,
float, double, and CObject* (user-defined data type).
MFC library classes that are not derived from CObject, such as CString and CRect,
have their own overloaded insertion and extraction operators so that they can be
used with CArchive.
An example for implementation of the Serialize() function
void COurClass::Serialize(CArchive& ar)
{
// COurClass objects contain member variables width
// and Height that are of type
WORD w, h; // temporary conversion variables
if (ar.IsStoring())
{
ar << m_nWidth;
ar << m_nHeight;
}
else
{
ar >> m_nWidth;
ar >> m_nHeight;
}
}
|