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 ...
随机推荐
- 重新开始学习javase_集合_Map
一,Map之HashMap(转:http://blog.csdn.net/zheng0518/article/details/42197049) 1. HashMap概述: HashMap是基于 ...
- java_annotation_02
通过反射取得Annotation 在一上节中,我们只是简单的创建了Annotation,如果要让一个Annotation起作用,则必须结合反射机制,在Class类上存在以下几种于Annotation有 ...
- 嘟!数字三角形 W WW WWW集合!
哔!数字三角形全体集合! 数字三角形!到! 数字三角形W!到! 数字三角形WW!到! 数字三角形WWW!到! --------------------------------------------- ...
- C++11中int,float,double与string的转化
在C++11中可以使用std::to_string()函数将数值转换为string格式,十分方便. 以下部分来选自cplusplus.com. std::to_string string to_str ...
- linux变量心得
前一段时间学习了一下linux的变量,现在总结有3点需要特别注意: linux变量和C/C++变量的区别 linux变量的引用 linux变量特有的命令替换 先说第一点,linux变量更像是宏定义,只 ...
- Android学习----ADB
adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具.a ...
- Shell test命令
Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值.字符和文件三个方面的测试. 数值测试 参数 说明 -eq 等于则为真 -ne 不等于则为真 -gt 大于则为真 -ge 大于等于 ...
- jqgrid设置单元格数据
$("#gridid").jqGrid('setCell',rowid,icol,data); rowid为行ID,jqgrid内置的那个,从1开始 icol为列索引,从0开始, ...
- winform批量查询单号剔除重复
//查询分单函数 private string GetQueryInSubbillNo() { string strSubbillNO = " ...
- js 鼠标双击滚动单击停止
<!DOCTYPE html> <html> <head> <title>双击滚动代码</title> <script src=&qu ...