MFC序列化
Book.h
#include <afxwin.h>

class CBook : public CObject
{
DECLARE_SERIAL(CBook)

public:
CString m_strName;
CString m_strAuthor;
CString m_strPress;

public:
CBook()
{
// 要使CBook对象实现序列化,必须定义默认构造函数
}

CBook(CString strName, CString strAuthor, CString strPress)
{
m_strName = strName; // 书名
m_strAuthor = strAuthor; // 作者
m_strPress = strPress; // 出版社
}

void Serialize(CArchive &); // 重载基类的Serialize函数
};

Book.cpp

#include "Book.h"

IMPLEMENT_SERIAL(CBook, CObject, 1)

void CBook::Serialize(CArchive &ar)
{
CObject::Serialize(ar);
if(ar.IsStoring())
{
ar << m_strName << m_strAuthor << m_strPress;
}
else
{
ar >> m_strName >> m_strAuthor >> m_strPress;
}
}
主程序
CBook book1("Programming Windows", "Charles Petzold", "Microsoft Press");
CBook book2("Programming Windows with MFC", "Jeff Prosise", "Microsoft Press");
int nCount = 2;

// 序列化
CFile fileStore(_T("mybooks.dat"), CFile::modeWrite | CFile::modeCreate);
CArchive arStore(&fileStore, CArchive::store);
CBook* pBooksStore[2] = { &book1, &book2 };
arStore << nCount;
for (int i=0; i<nCount; ++i)
{
arStore << pBooksStore[i];
}
fileStore.Close();

// 反序列化
CFile fileLoad(_T("mybooks.dat"), CFile::modeRead);
CArchive arLoad(&fileLoad, CArchive::load);
arLoad >> nCount;
CBook* pBooksLoad = new CBook[nCount];
for (int i=0; i<nCount; ++i)
{
arLoad >> pBooksLoad[i];
}
fileLoad.Close();
MFC序列化注意事项:
·要想使某一类型能被序列化,在它类型声明文件(头文件,Book.h)和类型定义文件(Book.cpp)中要分别使用DECLARE_SERIAL和IMPLEMENT_SERIAL宏。
·能被序列化的只能指针型,不能是值型(pBooksStore数组中包含的就是两个CBook类型的指针)。
#include <afxwin.h>
class CBook : public CObject
{
DECLARE_SERIAL(CBook)
public:
CString m_strName;
CString m_strAuthor;
CString m_strPress;
public:
CBook()
{
// 要使CBook对象实现序列化,必须定义默认构造函数
}
CBook(CString strName, CString strAuthor, CString strPress)
{
m_strName = strName; // 书名
m_strAuthor = strAuthor; // 作者
m_strPress = strPress; // 出版社
}
void Serialize(CArchive &); // 重载基类的Serialize函数
};
Book.cpp
#include "Book.h"
IMPLEMENT_SERIAL(CBook, CObject, 1)
void CBook::Serialize(CArchive &ar)
{
CObject::Serialize(ar);
if(ar.IsStoring())
{
ar << m_strName << m_strAuthor << m_strPress;
}
else
{
ar >> m_strName >> m_strAuthor >> m_strPress;
}
}主程序
CBook book1("Programming Windows", "Charles Petzold", "Microsoft Press");
CBook book2("Programming Windows with MFC", "Jeff Prosise", "Microsoft Press");
int nCount = 2;
// 序列化
CFile fileStore(_T("mybooks.dat"), CFile::modeWrite | CFile::modeCreate);
CArchive arStore(&fileStore, CArchive::store);
CBook* pBooksStore[2] = { &book1, &book2 };
arStore << nCount;
for (int i=0; i<nCount; ++i)
{
arStore << pBooksStore[i];
}
fileStore.Close();
// 反序列化
CFile fileLoad(_T("mybooks.dat"), CFile::modeRead);
CArchive arLoad(&fileLoad, CArchive::load);
arLoad >> nCount;
CBook* pBooksLoad = new CBook[nCount];
for (int i=0; i<nCount; ++i)
{
arLoad >> pBooksLoad[i];
}
fileLoad.Close();MFC序列化注意事项:
·要想使某一类型能被序列化,在它类型声明文件(头文件,Book.h)和类型定义文件(Book.cpp)中要分别使用DECLARE_SERIAL和IMPLEMENT_SERIAL宏。
·能被序列化的只能指针型,不能是值型(pBooksStore数组中包含的就是两个CBook类型的指针)。


浙公网安备 33010602011771号