Note:
In the following example, obj1 and obj2 all require declare_serial and implement_serial operations. Or you can
Is Basic Windows type, and cstring.
Read files
Cfile file;
Cfileexception Fe;
// Open the file in Read mode
If (! File. Open (filename, cfile: moderead, & Fe ))
{
Fe. reporterror ();
Return;
}
// Construct a carchive object
Carchive AR (& file, carchive: load );
Ar> obj1> obj2> obj3....> objn;
Ar. Flush ();
// Close the file stream after reading
Ar. Close ();
File. Close ();
Write files
Cfile file;
Cfileexception Fe;
// Open the file in Read mode
If (! File. Open (filename, cfile: modewrite | cfile: modecreate, & Fe ))
{
Fe. reporterror ();
Return;
}
// Construct a carchive object
Carchive AR (& file, carchive: Store );
Ar <obj1 <obj2 <obj3.... <objn;
Ar. Flush ();
// Close the file stream after writing.
Ar. Close ();
File. Close ();
Specific implementation of OBJ:
Create a new ctestarchive class that inherits from the cobject class and contains the member cstring m_string;
Add declare_serial (ctestarchive)
Implement_serial (ctestarchive, cobject, 1)
Add a virtual function:
Void ctestarchive: serialize (carchive & AR)
{
Cobject: serialize (AR );
If (AR. isloading ())
Ar> m_string;
Else
Ar <m_string;
}
In the main program, after opening the file and carchive, you can call the serialize (AR) method of the instance.