A simple example of MFC serialization (serial)

Source: Internet
Author: User

We can simply save some text content as a file. however, it is complicated to save some Class Object Information and initialize a Class Object with that information during reading. MFC provides a function called serialization. you can convert an object information into a binary file (or an xml file) and save it. the following is a simple example. If there is a CArwen class, instantiate an object and serialize it. finally, deserialization is performed to read the saved values and initialize them to an object. 1. first define the class CArwen // header file class CArwen: public CObject {DECLARE_SERIAL (CArwen) public: virtual void Serialize (CArchive & ar); int age; CString name ;}; // cpp file IMPLEMENT_SERIAL (CArwen, CObject, 1) // 1 is the version number. You can specify a value void CArwen: Serialize (CArchive & ar) {if (ar. isStoring () {// serialize and save the information. CArchive: store corresponds to this ar <age; ar <name;} else // deserialization, read information {ar> age; ar> name ;}} 2. test procedure # include "CArwen. h "CString filePath = _ T (" D: \ app \ Arwen. dat "); // serialize the object information stored in the file void TestSerializeArwen () // Save the information {CArwen arwen; arwen. age = 24; arwen. name = _ T ("weiwenhp); // If unicode is not used, _ T CFile fi; fi is not used. open (filePath, CFile: modeCreate | CFile: modeWrite); // if the file does not exist, create one. Otherwise, write data to the file www.2cto.com CArchive ar (& fi, CArchive :: store); awen. serialize (ar); // Serialize the object ar. close (); fi. close ();} void TestReadSerilizedArwn () // read information {CArwen arwen; CFile fi; fi. open (filePath, CFile: Read); CArchive ar (& fi, CArchive: load); awen. serialize (ar); // deserialization object ar. close (); fi. close ();}

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.