文档与串行化

来源:互联网 发布:为什么淘宝那么卡 编辑:程序博客网 时间:2024/04/29 12:02

CArchive没有基类,CArchive类允许你保存一个复杂的对象网络在一个永久的二进制形式上,通常是一个磁盘存储器。在一个对象被删除之后保持持久性,以后你可以从持久的存储器上加载这一对象,在内存中重新构造它们。使数据持久性的这个过程叫做串行化。

void CGraphicView::OnFileWrite()

{

         CFilefile("1.txt",CFile::modeCreate | CFile::modeWrite);

         //构造一个CArchive对象

         CArchivear(&file,CArchive::store);

         //定义一些变量

         inti=4;

         charch='a';

         floatf=1.3f;

         //定义一个CString对象类型

         CStringstr("http://www.sunxin.org");

         ar<<i<<ch<<f<<str;

}

 

void CGraphicView::OnFileRead()

{

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

         CArchivear(&file,CArchive::load);

         inti;

         charch;

         floatf;

         //用来保存我们所读取的数据

         CStringstr;

         //将我们所读取的数据格式化到CString对象当中,然后用MessageBox显示出来

         CStringstrResult;

   //加载数据可以用它重载的提取操作符,

         //保存的时候按照什么样的数据,提取的时候也要按照什么样的数据

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

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

         MessageBox(strResult);

}

//总结:利用CArchive可以保存基本类型的数据,还可以保存CString的对象

 

2.可以在CGraphicDoc::OnNewDocument()中设置文档的标题:

   //设置文档的标题:

         SetTitle("http://www.sunxin.org");

 

3. Serialize:

现象:当我们在保存之后再次打开,它并不会弹出消息框:

void CGraphicDoc::Serialize(CArchive&ar)

{

         if(ar.IsStoring())

         {

                   //TODO: add storing code here

                   inti=5;

                   charch='b';

                   floatf=1.2f;

                   CStringstr("http://www.sunxin.org");

                   ar<<i<<ch<<f<<str;

         }

         else

         {

                   //TODO: add loading code here

                   inti;

                   charch;

                   floatf;

                   CStringstr;

                   CStringstrResult;

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

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

                   AfxMessageBox(strResult);

         }

}

 

出现的问题:Project->Add to Project->files出现异常。

原创粉丝点击