vc++上的MFC的对象序列化和反序列化

时间:2023-03-09 16:13:12
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;
  }
 }

序列化后的数据

vc++上的MFC的对象序列化和反序列化

  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. }