扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
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(); |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者