注意点:
 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;
  }
 }

序列化后的数据

  1. //Person.h
  2. #pragma once
  3. #include <afx.h>
  4. #include <string>
  5. #include <atlstr.h>
  6. using namespace std;
  7. class Person: public CObject
  8. {
  9. private:
  10. //注意MFC 不支持 标准std:string对象序列化, boost库支持std:string
  11. CString name;
  12. int age;
  13. char sex;
  14. public:
  15. DECLARE_SERIAL( Person )
  16. Person(void);
  17. Person(CString name, int age, char sex);
  18. virtual ~Person(void);
  19. virtual void Serialize(CArchive& ar);
  20. void setName(CString pName);
  21. CString getName();
  22. void setAge(int age);
  23. int getAge();
  24. void setSex(char sex);
  25. char getSex();
  26. };
  27. //Person.cpp
  28. #include "StdAfx.h"
  29. #include "Person.h"
  30. #include <afx.h>
  31. #include <string>
  32. //必须写出实现宏
  33. IMPLEMENT_SERIAL(Person, CObject, VERSIONABLE_SCHEMA | 2)
  34. Person::Person(void)
  35. {
  36. }
  37. Person::Person( CString name, int age, char sex )
  38. {
  39. this->name = name;
  40. this->age = age;
  41. this->sex = sex;
  42. }
  43. Person::~Person(void)
  44. {
  45. }
  46. void Person::setName(  CString name)
  47. {
  48. this->name = name;
  49. }
  50. CString Person::getName()
  51. {
  52. return this->name;
  53. }
  54. void Person::setAge( int age )
  55. {
  56. this->age = age;
  57. }
  58. int Person::getAge()
  59. {
  60. return this->age;
  61. }
  62. void Person::setSex( char sex )
  63. {
  64. this->sex = sex;
  65. }
  66. char Person::getSex()
  67. {
  68. return this->sex;
  69. }
  70. void Person::Serialize( CArchive& ar )
  71. {
  72. CObject::Serialize(ar);
  73. //关键代码
  74. if(ar.IsStoring()) {
  75. //序列化
  76. ar << this->age << this->sex << this->name;
  77. } else {
  78. //反序列化
  79. ar >> this->age >> this->sex >> this->name;
  80. }
  81. }
  82. // main.cpp : 定义控制台应用程序的入口点。
  83. #include "stdafx.h"
  84. #include <tchar.h>
  85. #include <afx.h>
  86. #include <iostream>
  87. using namespace std;
  88. int _tmain(int argc, _TCHAR* argv[])
  89. {
  90. Person person;
  91. person.setAge(20);
  92. person.setName("zhangsan");
  93. person.setSex('1');
  94. CFile myFile(_T("c:/person.ser"), CFile::modeCreate | CFile::modeReadWrite);
  95. // Create a storing archive.
  96. CArchive arStore(&myFile, CArchive::store);
  97. // Write the object to the archive
  98. arStore.WriteObject(&person);
  99. arStore.Flush();
  100. // Close the storing archive
  101. arStore.Close();
  102. // Create a loading archive.
  103. myFile.SeekToBegin();
  104. CArchive arLoad(&myFile, CArchive::load);
  105. // Verify the object is in the archive.
  106. Person* p = (Person*) arLoad.ReadObject(person.GetRuntimeClass());
  107. arLoad.Close();
  108. //wcout << "姓名:" << name.GetBuffer(name.GetLength()) << endl;
  109. CString name = p->getName();
  110. wchar_t* pch = name.GetBuffer(0);
  111. wcout << "姓名:" << pch << endl;
  112. name.ReleaseBuffer(); //注意内在释放
  113. cout << "性别:" << p->getSex() << endl;
  114. cout << "年龄:" << p->getAge() << endl;
  115. delete p;
  116. return 0;
  117. }

vc++上的MFC的对象序列化和反序列化的更多相关文章

  1. java 对象序列化与反序列化

    Java序列化与反序列化是什么? 为什么需要序列化与反序列化? 如何实现Java序列化与反序列化? 本文围绕这些问题进行了探讨. 1.Java序列化与反序列化  Java序列化是指把Java对象转换为 ...

  2. C#对象序列化与反序列化zz

      C#对象序列化与反序列化(转载自:http://www.cnblogs.com/LiZhiW/p/3622365.html) 1. 对象序列化的介绍........................ ...

  3. C#对象序列化与反序列化

    C#对象序列化与反序列化(转载自:http://www.cnblogs.com/LiZhiW/p/3622365.html) 1. 对象序列化的介绍.......................... ...

  4. Java学习笔记——IO操作之对象序列化及反序列化

    对象序列化的概念 对象序列化使得一个程序可以把一个完整的对象写到一个字节流里面:其逆过程则是从一个字节流里面读出一个事先存储在里面的完整的对象,称为对象的反序列化. 将一个对象保存到永久存储设备上称为 ...

  5. Java之对象序列化和反序列化

    一.对象序列化和反序列化存在的意义: 当你创建对象,只要你需要,他就一直存在,但当程序结束,对象就会消失,但是存在某种情况,如何让程序在不允许的状态,仍然保持该对象的信息.并在下次程序运行的时候使用该 ...

  6. Java 序列化 对象序列化和反序列化

    Java 序列化 对象序列化和反序列化 @author ixenos 对象序列化是什么 1.对象序列化就是把一个对象的状态转化成一个字节流. 我们可以把这样的字节流存储为一个文件,作为对这个对象的复制 ...

  7. Java对象序列化与反序列化

    对象序列化的目标是将对象保存在磁盘中或者在网络中进行传输.实现的机制是允许将对象转为与平台无关的二进制流. java中对象的序列化机制是将允许对象转为字节序列.这些字节序列可以使Java对象脱离程序存 ...

  8. FastJson实现复杂对象序列化与反序列化

    原文:http://blog.csdn.net/xqhadoop/article/details/62217954 一.认识FastJson 1.优势 fastjson是目前java语言中最快的jso ...

  9. Java Io 对象序列化和反序列化

    Java 支持将任何对象进行序列化操作,序列化后的对象文件便可通过流进行网络传输. 1.      对象序列化就是将对象转换成字节序列,反之叫对象的反序列化 2.      序列化流ObjectOut ...

随机推荐

  1. 重新开始学习javase_集合_Map

    一,Map之HashMap(转:http://blog.csdn.net/zheng0518/article/details/42197049) 1.    HashMap概述: HashMap是基于 ...

  2. java_annotation_02

    通过反射取得Annotation 在一上节中,我们只是简单的创建了Annotation,如果要让一个Annotation起作用,则必须结合反射机制,在Class类上存在以下几种于Annotation有 ...

  3. 嘟!数字三角形 W WW WWW集合!

    哔!数字三角形全体集合! 数字三角形!到! 数字三角形W!到! 数字三角形WW!到! 数字三角形WWW!到! --------------------------------------------- ...

  4. C++11中int,float,double与string的转化

    在C++11中可以使用std::to_string()函数将数值转换为string格式,十分方便. 以下部分来选自cplusplus.com. std::to_string string to_str ...

  5. linux变量心得

    前一段时间学习了一下linux的变量,现在总结有3点需要特别注意: linux变量和C/C++变量的区别 linux变量的引用 linux变量特有的命令替换 先说第一点,linux变量更像是宏定义,只 ...

  6. Android学习----ADB

    adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具.a ...

  7. Shell test命令

    Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值.字符和文件三个方面的测试. 数值测试 参数 说明 -eq 等于则为真 -ne 不等于则为真 -gt 大于则为真 -ge 大于等于 ...

  8. jqgrid设置单元格数据

    $("#gridid").jqGrid('setCell',rowid,icol,data); rowid为行ID,jqgrid内置的那个,从1开始 icol为列索引,从0开始, ...

  9. winform批量查询单号剔除重复

    //查询分单函数        private string GetQueryInSubbillNo()        {            string strSubbillNO = " ...

  10. js 鼠标双击滚动单击停止

    <!DOCTYPE html> <html> <head> <title>双击滚动代码</title> <script src=&qu ...