vc++上的MFC的对象序列化和反序列化
注意点:
1. 必须类型序列化声明
DECLARE_SERIAL( Person )
2. 必须写出实现宏
IMPLEMENT_SERIAL(Person, CObject, VERSIONABLE_SCHEMA | 2)
3. 重写CObject中的Serialize函数
void Person::Serialize( CArchive& ar )
{
CObject::Serialize(ar);
//关键代码
if(ar.IsStoring()) {
//序列化
ar << this->age << this->sex << this->name;
} else {
//反序列化
ar >> this->age >> this->sex >> this->name;
}
}
序列化后的数据
- //Person.h
- #pragma once
- #include <afx.h>
- #include <string>
- #include <atlstr.h>
- using namespace std;
- class Person: public CObject
- {
- private:
- //注意MFC 不支持 标准std:string对象序列化, boost库支持std:string
- CString name;
- int age;
- char sex;
- public:
- DECLARE_SERIAL( Person )
- Person(void);
- Person(CString name, int age, char sex);
- virtual ~Person(void);
- virtual void Serialize(CArchive& ar);
- void setName(CString pName);
- CString getName();
- void setAge(int age);
- int getAge();
- void setSex(char sex);
- char getSex();
- };
- //Person.cpp
- #include "StdAfx.h"
- #include "Person.h"
- #include <afx.h>
- #include <string>
- //必须写出实现宏
- IMPLEMENT_SERIAL(Person, CObject, VERSIONABLE_SCHEMA | 2)
- Person::Person(void)
- {
- }
- Person::Person( CString name, int age, char sex )
- {
- this->name = name;
- this->age = age;
- this->sex = sex;
- }
- Person::~Person(void)
- {
- }
- void Person::setName( CString name)
- {
- this->name = name;
- }
- CString Person::getName()
- {
- return this->name;
- }
- void Person::setAge( int age )
- {
- this->age = age;
- }
- int Person::getAge()
- {
- return this->age;
- }
- void Person::setSex( char sex )
- {
- this->sex = sex;
- }
- char Person::getSex()
- {
- return this->sex;
- }
- void Person::Serialize( CArchive& ar )
- {
- CObject::Serialize(ar);
- //关键代码
- if(ar.IsStoring()) {
- //序列化
- ar << this->age << this->sex << this->name;
- } else {
- //反序列化
- ar >> this->age >> this->sex >> this->name;
- }
- }
- // main.cpp : 定义控制台应用程序的入口点。
- #include "stdafx.h"
- #include <tchar.h>
- #include <afx.h>
- #include <iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- Person person;
- person.setAge(20);
- person.setName("zhangsan");
- person.setSex('1');
- CFile myFile(_T("c:/person.ser"), CFile::modeCreate | CFile::modeReadWrite);
- // Create a storing archive.
- CArchive arStore(&myFile, CArchive::store);
- // Write the object to the archive
- arStore.WriteObject(&person);
- arStore.Flush();
- // Close the storing archive
- arStore.Close();
- // Create a loading archive.
- myFile.SeekToBegin();
- CArchive arLoad(&myFile, CArchive::load);
- // Verify the object is in the archive.
- Person* p = (Person*) arLoad.ReadObject(person.GetRuntimeClass());
- arLoad.Close();
- //wcout << "姓名:" << name.GetBuffer(name.GetLength()) << endl;
- CString name = p->getName();
- wchar_t* pch = name.GetBuffer(0);
- wcout << "姓名:" << pch << endl;
- name.ReleaseBuffer(); //注意内在释放
- cout << "性别:" << p->getSex() << endl;
- cout << "年龄:" << p->getAge() << endl;
- delete p;
- return 0;
- }
vc++上的MFC的对象序列化和反序列化的更多相关文章
- java 对象序列化与反序列化
Java序列化与反序列化是什么? 为什么需要序列化与反序列化? 如何实现Java序列化与反序列化? 本文围绕这些问题进行了探讨. 1.Java序列化与反序列化 Java序列化是指把Java对象转换为 ...
- C#对象序列化与反序列化zz
C#对象序列化与反序列化(转载自:http://www.cnblogs.com/LiZhiW/p/3622365.html) 1. 对象序列化的介绍........................ ...
- C#对象序列化与反序列化
C#对象序列化与反序列化(转载自:http://www.cnblogs.com/LiZhiW/p/3622365.html) 1. 对象序列化的介绍.......................... ...
- Java学习笔记——IO操作之对象序列化及反序列化
对象序列化的概念 对象序列化使得一个程序可以把一个完整的对象写到一个字节流里面:其逆过程则是从一个字节流里面读出一个事先存储在里面的完整的对象,称为对象的反序列化. 将一个对象保存到永久存储设备上称为 ...
- Java之对象序列化和反序列化
一.对象序列化和反序列化存在的意义: 当你创建对象,只要你需要,他就一直存在,但当程序结束,对象就会消失,但是存在某种情况,如何让程序在不允许的状态,仍然保持该对象的信息.并在下次程序运行的时候使用该 ...
- Java 序列化 对象序列化和反序列化
Java 序列化 对象序列化和反序列化 @author ixenos 对象序列化是什么 1.对象序列化就是把一个对象的状态转化成一个字节流. 我们可以把这样的字节流存储为一个文件,作为对这个对象的复制 ...
- Java对象序列化与反序列化
对象序列化的目标是将对象保存在磁盘中或者在网络中进行传输.实现的机制是允许将对象转为与平台无关的二进制流. java中对象的序列化机制是将允许对象转为字节序列.这些字节序列可以使Java对象脱离程序存 ...
- FastJson实现复杂对象序列化与反序列化
原文:http://blog.csdn.net/xqhadoop/article/details/62217954 一.认识FastJson 1.优势 fastjson是目前java语言中最快的jso ...
- Java Io 对象序列化和反序列化
Java 支持将任何对象进行序列化操作,序列化后的对象文件便可通过流进行网络传输. 1. 对象序列化就是将对象转换成字节序列,反之叫对象的反序列化 2. 序列化流ObjectOut ...
随机推荐
- C# Double String互转
/// <summary> /// str转金额 元 /// </summary> /// <param name="money"></p ...
- js单条新闻向上滚动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- MyBatis学习笔记(3)—— 利用mybatis灌入假数据
由于第三方厂商未能按时提供实时数据,故需要纯手动导入一些实时数据,用于统计分析.正好最近自己学习了mybatis .因此使用mybatis 配置一个select.insert 的简单操作语句,用于灌入 ...
- grails通过findBy或findBy查找的结果集进行排序
原文:http://grails.org/doc/2.3.x/ref/Domain%20Classes/list.html list Purpose Lists instances of the do ...
- JQUERY1.9学习笔记 之基本过滤器(九) 小于选择器
小于选择器 jQuery( ":lt(index)" ) jQuery( ":lt(-index)" ) 描述:选择所有小于指定下标的元素. <!DOCT ...
- Smarty 模板引擎 fetch()和display()函数的区别?
Smarty模板函数里面有这样一个方法:fetch("template.htm"),他和display("template.htm");最大的不同就是fetch ...
- Yii 框架里数据库操作详解
增:1 第一种 $post=new Post; $post->title='sample post'; $post->content='content for the sample pos ...
- js 获取鼠标位置坐标
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- Delphi-Copy 函数
函数名称 Copy 所在单元 System 函数原型 1 function Copy ( Source : string; StartChar, Count : Integer ) : string ...
- 在动态引用DLL-A中,当参数是个实体,而实体的属性在另一个DLL-B中。。我们需要得到A这个实体并将其赋值,并将赋值的实体传人DLL-A的方法中。
string strPath = HttpContext.Current.Server.MapPath("/开放式DLL"); DirectoryInfo df = new Dir ...