VC++ chap13 文档与串行化

Lesson 13 文档与串行化

13.1使用CArchive类对文件进行读写操作

//让对象数据持久性的过程称之为串行化,或者序列化 

void CGraphicView::OnFileWrite()

{

       // TODO: Add your command handler code here

       CFile file("1.txt",CFile::modeCreate|CFile::modeWrite);    //build CFile object

       CArchive ar(&file,CArchive::store);                   //build archive object

       int i=4;

       char ch='a';

       float f=1.3f;

       CString str("aliceandrabbit");

       ar<<i<<ch<<f<<str;      //save dates; 

void CGraphicView::OnFileRead()

{

       // TODO: Add your command handler code here

       CFile file("1.txt",CFile::modeRead);

       CArchive ar(&file,CArchive::load);

       int i;

       char ch;

       float f;

       CString str;

       CString strResult; 

       ar>>i>>ch>>f>>str;

       strResult.Format("%d,%c,%f,%s",i,ch,f,str);

       MessageBox(strResult);

}

13.2MFC框架程序提供的文件新建功能 

13.3 文档串行化 

13.4可串行化的类

先要使类具有串行化

1,拷贝lesson11的CGraph对象,将其修改为从CObject派生。

2,重载Serialzie函数(在CGraph类中添加)

3,在声明CGraph类时,使用DECLARE_SERIAL宏,即在graph.h头文件中,在类定义的内部添加

DECLARE_SERIAL(CGraph)

4,该类需要不带参数的构造函数

5,为CGraph类在实现文件中使用IMPLEMENT_SERIAL宏,即在CGraph类的构造函数的定义前添加:IMPLEMENT_SERIAL(CGraph,CObject,1)

至此,CGraph类就支持串行化了。

然后为此类添加一个图形绘制函数:Draw,从而将图形数据和图形绘制封装在一个类中

原文地址:https://www.cnblogs.com/aprilapril/p/3457298.html