Serialization (serialization)

Source: Internet
Author: User

Serialization is a mechanism provided by Microsoft for file I/O to objects, and the mechanism is well applied in frame/document (documents)/view mode. Many people are not very clear about what serialization is, how to make the object serializable, and how to use the serialization feature. This paper attempts to make a simple explanation of serialization. Because I use the serialization function is not much, the deficiencies please understand.

File reading and writing in MFC Framework/document/view structure

CFile is the base class for all file classes in the MFC class library. All of the file I/O features provided by MFC are related to this class. In many cases, people like to call Cfile::write/writehuge directly to write files, call Cfile::read/readhuge to read files. Such file I/O in fact and do not use MFC file I/O does not differ, even with the previous ANSI C file I/O is not much different, the difference is not only the API of the call.

When you start learning C + +, you must be familiar with cin/cout, which use the very clear << and >> operators for I/O, using the format://示例代码1
int i;
cin >> i;
//here do something to object i
cout << i;

With the benefit of I/O in this way, with operator overloading, you can read and write to a series of objects with a single statement without distinguishing the specific types of objects. MFC provides class CArchive, implements the overloads of operators << and >>, and wants file I/O in front of Cin and cout. In conjunction with the CFile class, not only does the file read and write to the simple type such as int/float, but also the file read and write to the serializable object (Serializable Objects, described later).

In general, the process of reading an object using CArchive is as follows:

//示例代码2
//定义文件对象和文件异常对象
CFile file;
CFileException fe;
//以读方式打开文件
if(!file.Open(filename,CFile::modeRead,&fe))
{
fe.ReportError();
return;
}

//构建CArchive 对象
CArchive ar(&file,CArchive::load);
ar >> obj1>>obj2>>obj3...>>objn;
ar.Flush();
//读完毕,关闭文件流
ar.Close();
file.Close();

The process of writing an object using CArchive is as follows://示例代码3
//定义文件对象和文件异常对象
CFile file;
CFileException fe;
//以读方式打开文件
if(!file.Open(filename,CFile::modeWrite|CFile::modeCreate,&fe))
{
fe.ReportError();
return;
}

//构建CArchive 对象
CArchive ar(&file,CArchive::load);
ar << obj1<<obj2<<obj3...<<objn;
ar.Flush();
//写完毕,关闭文件流
ar.Close();
file.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.