Use carchive to save objects serially

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.