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


 

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MFC(Microsoft Foundation Classes)是微软提供的一套用于开发Windows桌面应用程序的C++类库。在MFC中,序列化(Serialization)是指将对象的状态转换为可以存储或传输的格式,而反序列化(Deserialization)则是将存储或传输的数据恢复为对象的状态。 MFC提供了一些类和宏来支持序列化反序列化操作,主要涉及以下几个类: 1. CObject类:所有支持序列化MFC类都继承自CObject类。它提供了一些虚拟函数,如Serialize用于实现对象序列化反序列化操作。 2. CArchive类:用于创建和操作存储或传输数据的存档文件。CArchive类提供了Serialize函数,用于将对象的状态写入存档文件或从存档文件中读取对象的状态。 3. CFile类:用于对文件进行读写操作。它提供了Open、Read、Write等函数来操作文件。CArchive类可以使用CFile类来读写数据。 下面是一个简单的示例代码,演示了如何在MFC中进行序列化反序列化操作: ```cpp // 假设有一个自定义的类MyData,需要支持序列化反序列化 class MyData : public CObject { public: int m_value; // 实现Serialize函数来进行对象序列化反序列化操作 void Serialize(CArchive& ar) { if (ar.IsStoring()) // 写入数据 ar << m_value; else // 读取数据 ar >> m_value; } }; // 序列化操作 void SerializeData() { CFile file(_T("data.dat"), CFile::modeCreate | CFile::modeWrite); CArchive ar(&file, CArchive::store); MyData data; data.m_value = 42; data.Serialize(ar); // 将对象的状态写入存档文件 ar.Close(); file.Close(); } // 反序列化操作 void DeserializeData() { CFile file(_T("data.dat"), CFile::modeRead); CArchive ar(&file, CArchive::load); MyData data; data.Serialize(ar); // 从存档文件中读取对象的状态 ar.Close(); file.Close(); // 使用反序列化后的对象... int value = data.m_value; } ``` 在上述示例中,Serialize函数根据ar对象的状态进行读写操作,将m_value成员变量的值进行序列化反序列化。通过创建CFile和CArchive对象,可以实现对存档文件的读写操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值