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 ...
随机推荐
- ubuntu远程连接
apt-cache search openssh-server //直接用apt-get install openssh-server安装.记不清包名字时可用apt-cache search o ...
- jquery工具函数browser() 辨别浏览器
1.browser属性不是一个函数是一个全局对象,可以辨别客户端浏览器. 2.属性1:$.browser.msie如果返回true则客户端浏览器是ie.相似的$.browser.safari返回tru ...
- Python的多线程实现
概述 Python虚拟机使用GIL(Global Interpreter Lock,全局解释器锁)来实现互斥线程对共享资源的访问,暂时无法利用多处理器的优势. Python中,thread和threa ...
- Javascript中的onclick事件
示例: <script type="text/javascript"> function onSubmit() { alert("保存数据"); } ...
- net Core 使用MyCat分布式数据库,实现读写分离
net Core 使用MyCat分布式数据库,实现读写分离 目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 MyCat2.0版本很快就发布了,关于MyCat的动态和一些问题, ...
- OPNET安装要点
最近在做一点网络的仿真工作,需要用到OPNET这个工具,安装了一早上终于安装好了.安装过程如下: 1.安装visual studio 2010:其他版本如vs2005, vs2008也是可以的.vs2 ...
- 机器学习(4)之Logistic回归
机器学习(4)之Logistic回归 1. 算法推导 与之前学过的梯度下降等不同,Logistic回归是一类分类问题,而前者是回归问题.回归问题中,尝试预测的变量y是连续的变量,而在分类问题中,y是一 ...
- Zend Cache的学习和实例
前一段时间,公司让我组织一下关于Zend Cache的培训. 培训的具体内容有: 前端core缓存 前端Output缓存 前端Function缓存 前端Class缓存 前端File缓存 前端Page缓 ...
- mongodb3.0 db.addUser报错
> db.addUser("ydkt","ydkt") --19T09:: E QUERY TypeError: Property 'addUser' o ...
- SCALA常规练习A
package com.hengheng.scala import scala.util.control.Breaks._ import scala.util.Random object Applic ...