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 ...
随机推荐
- 无法升级数据库....因为此版本的 SQL Server 不支持该数据库的非发布版本(539) 解决方案
使用SQL2012附加一个数据库时报出了以下错误:“无法升级数据库....因为此版本的 SQL Server 不支持该数据库的非发布版本(539).不能打开与此版本的 sqlserver.exe 不兼 ...
- Android单位度量
px(像素):屏幕上的点. in(英寸):长度单位.mm(毫米):长度单位.pt(磅):1/72英寸.dp(与密度无关的像素):一种基于屏幕密度的抽象单位.在每英寸160点的显示器上,1dp = 1p ...
- 【COGS1384】鱼儿仪仗队
[题目描述] Jzyz的池塘里有很多条鱼,鱼儿们现在决定组成一个仪仗队.现在备选的N(1 <= N <= 100,000)条鱼排成了一条直线,并且按照亲近关系排的队伍,鱼儿的顺序不能改变, ...
- 关于jQuery表单校验的应用
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- PHP获取当前文件路径信息的方法
文件名 test.php 1.__FILE__ 获取 “路径 + 文件名” : /var/www/test/test.php echo __FILE__; //取得当前文件的路径:用魔术常量 __ ...
- python抓取网页图片
本人比较喜欢海贼王漫画,所以特意选择了网站http://www.mmonly.cc/ktmh/hzw/list_34_2.html来抓取海贼王的图片. 因为是刚刚学习python,代码写的不好,不要喷 ...
- Day2 数据类型
1.数字:int(整型) 32位机器:-2**31~2**31-1 64位机器:-2**63~2**63-1 float(浮点型) 2.布尔值 真或假 1或0 bool(0) 3.字符串 name = ...
- django 序列化json问题
from django.core import serializers @login_required def ajax_get_data(request): json_data = serializ ...
- sql server 调优----索引未使用
SELECT TOP 1000o.name AS 表名, i.name AS 索引名, i.index_id AS 索引id, dm_ius.user_seeks AS 搜索次数, dm_ius.us ...
- ISO15693标准详细介绍
1.符合ISO/IEC 15693标准的信号接口部分的性能如下:1.1 工作频率 工作频率为13.56MKz±7KHz1.2 工作场强 工作场的最小值为0.15A/m,最大场为5A/m.1.3 调制 ...