⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 right17-3.htm

📁 Visual C++面向对象程序设计教程(配套习题资源)
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>第 17 章</title>
<base target="rbottom">
<style>
<!--
.右标题   { font-size: 10pt; color: #000080; text-indent: 0; margin: 0 }
.右内容   { font-size: 10pt; text-align: left; text-indent: 0; line-height: 100%; margin: 
               0 }
-->
</style>
</head>

<body>

<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="position: absolute; left: 4; top: -8"><img border="0" src="1.gif" width="63" height="70"></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                                          
&nbsp;&nbsp;&nbsp; <span style="background-color: #CCFF99">第 17 章</span><span style="background-color: #CCFF99">&nbsp;                                                 
</span>&gt;<span style="background-color: #CCFF99"> 第 3 节 </span>&gt;<span style="background-color: #CCFF99">   
 17.3 </span><span style="background-color: #CCFF99">文件处理</span></p>                  
<hr color="#008000" size="1">

<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"><font size="2"><font LANG="ZH-CN">在</font>MFC<font LANG="ZH-CN">中,程序的存取工作的核心内容就是如何实现这些对象的序列化。一个可以实现序列化的对象知道如何保存和载入它们自己的数据。</font></font></p>

<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">1.  
<font LANG="ZH-CN">对象序列化简述</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">序列化,简单地说就是向一个持久性的存储媒体棗如磁盘文件保存对象或读取对象的过程。可以实现序列化的类---即从</font>CObject<font LANG="ZH-CN">继承而来的类,有一个叫做</font>Serialize()<font LANG="ZH-CN">的成员函数,序列化工作主要是在这个函数当中进行的。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2">MFC<font LANG="ZH-CN">使用一个类型为</font>CArchive<font LANG="ZH-CN">的归档对象充当磁盘文件与程序中的对象的中介。归档对象总是与一个</font>CFile<font LANG="ZH-CN">对象相关联,从中获得一些进行序列化所需要的信息,比如说文件名、以及存取标志等。</font></font></p>

<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"> </p>

<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2"><font LANG="ZH-CN">对象序列化的主要工作就是将自己的成员变量或者当前状态保存起来。我们可以使用经过重载的流入和流出操作符直接向归档对象中保存或者取出成员变量的值,而将这些数据保存到磁盘文件中的工作由</font>CArchive<font LANG="ZH-CN">对象指示</font>CFile<font LANG="ZH-CN">对象来完成。</font></font></p>

<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"> </p>

<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2"><font LANG="ZH-CN">当用户在打开或保存拥有文档对象数据的文件或者使用文档对象的</font>Open<font LANG="ZH-CN">、</font>Save<font LANG="ZH-CN">、</font>Save  
As<font LANG="ZH-CN">菜单命令时,</font>MFC<font LANG="ZH-CN">便会自动调用</font>Serialize(  
)<font LANG="ZH-CN">函数。</font></font></p>

<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">使类实现序列化,需要五个步骤:</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">①</font> 
<font LANG="ZH-CN">从</font>Cobject<font LANG="ZH-CN">类或其派生类派生用户类。</font></font></p> 
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">②</font> 
<font LANG="ZH-CN">在类声明中使用</font>DECLARE_SERIAL<font LANG="ZH-CN">宏。</font></font></p> 
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">③</font> 
<font LANG="ZH-CN">重载</font>Serialize()<font LANG="ZH-CN">函数。</font></font></p> 
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">④</font> 
<font LANG="ZH-CN">定义不带参数的构造函数。</font></font></p> 
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">⑤</font> 
<font LANG="ZH-CN">在类实现文件中使用宏</font>IMPLEMENT_SERIAL<font LANG="ZH-CN">。</font></font></p> 
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"> </p>

<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2">2.  
<font LANG="ZH-CN">对象序列化</font></font></p>

<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">将例</font>17-1<font LANG="ZH-CN">文档类的变量</font>text<font LANG="ZH-CN">序列化的方法比较简单,只需要在</font>CTextDoc<font LANG="ZH-CN">的函数成员</font>Serialize(  
)<font LANG="ZH-CN">增加两条语句即可。代码如下:</font></font></p>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">void  
    CTextDoc::Serialize(CArchive&amp; ar)</font></p> 
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <blockquote>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">if  
      (ar.IsStoring( )) //<font LANG="ZH-CN">如果使用菜单命令“保存”或“另存为”时将数据存盘</font></font></p> 
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
      <blockquote>
        <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">ar&lt;&lt;text;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
        //<font LANG="ZH-CN">将数据存入文件中</font></font></p>
      </blockquote>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
      //<font LANG="ZH-CN">如果使用菜单命令“打开”时将磁盘数据导入变量中</font></font></p>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
      <blockquote>
        <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">ar&gt;&gt;text;&nbsp;&nbsp;&nbsp;&nbsp;  
        //<font LANG="ZH-CN">将磁盘文件导入变量中</font></font></p>
      </blockquote>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
    </blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
  </blockquote>
</blockquote>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"> </p>

<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>            

<p style="line-height: 100%; margin-top: 0; margin-bottom: 0">&nbsp;&nbsp; <span style="position: absolute; left: 34; top: 858"><a href="right17-2.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="position: absolute; left: 505; top: 865"><a href="right17-4.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></p>                    

</body>

</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -