vc++上的MFC的對象序列化和還原序列化

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.