📄 teach_road_35.htm
字号:
<html><head><meta http-equiv="Content-Type"content="text/html; charset=gb_2312-80"><title>VC Road</title><style type="text/css" MEDIA="screen"><!--a {text-decoration: underline;}a:hover {color:red; text-decoration:underline}small {color: navy; font-size: 12px ; line-height:16px;}h1 {color: navy; font-size: 12px ; line-height:16px;}
big {color: navy; font-size: 22px ; line-height:26px;}
td {color: navy; font-size: 12px ; line-height:16px;bgcolor="#FFCFA3";}--></style></head>
<body background="../img/ground.gif">
<p align="center"><a
HREF="http://www.0828.com/pay/cgi-bin/random.cgi?job=go&id=820"
target="_blank">
<img SRC="http://www.0828.com/pay/cgi-bin/random.cgi?id=820" BORDER="0"
width="468" height="60"></a><br>
<font size="2"><a href="http://www.0828.com/pay/index.html" target=_blank><img border="0"
src="http://www.0828.com/pay/code.gif" width="468" height="15"></a><br><small>你每点一下上面的广告我就能有更多的上网时间为大家寻找资料</small></font>
</p>
<p align=center><big>3.5 利用序列化进行文件读写</big></p><table border=0 align=center width=80%>
<tr><td>
<small>
<p>在很多应用中我们需要对数据进行保存,或是从介质上读取数据,这就涉及到文件的操作。我们可以利用各种文件存取方法完成这些工作,但MFC中也提供了一种读写文件的简单方法——“序列化”。序列化机制通过更高层次的接口功能向开发者提供了更利于使用和透明于字节流的文件操纵方法,举一个例来讲你可以将一个字串写入文件而不需要理会具体长度,读出时也是一样。你甚至可以对字符串数组进行操作。在MFC提供的可自动分配内存的类的支持下你可以更轻松的读/写数据。你也可以根据需要编写你自己的具有序列化功能的类。</p>
<p>序列化在最低的层次上应该被需要序列化的类支持,也就是说如果你需要对一个类进行序列化,那么这个类必须支持序列化。当通过序列化进行文件读写时你只需要该类的序列化函数就可以了。</p>
<p>怎样使类具有序列化功能呢?你需要以下的工作:
<ul>
<li>该类从CObject派生。</li>
<li>在类声明中包括DECLARE_SERIAL宏定义。</li>
<li>提供一个缺省的构造函数。</li>
<li>在类中实现Serialze函数</li>
<li>使用IMPLEMENT_SERIAL指明类名和版本号</li>
</ul>
</p>
<p>下面的代码建立了一个简单身份证记录的类,同时也能够支持序列化。
<pre>
in H
struct strPID
{
char szName[10];
char szID[16];
struct strPID* pNext;
};
class CAllPID : public CObject
{
public:
DECLARE_SERIAL(CAllPID)
CAllPID();
~CAllPID();
public:// 序列化相关
struct strPID* pHead;
//其他的成员函数
void Serialize(CArchive& ar);
};
in CPP
IMPLEMENT_SERIAL(CAllPID,CObject,1) // version is 1,版本用于读数据时的检测
void CAllPID::Serialize(CArchive& ar)
{
int iTotal;
if(ar.IsStoring())
{//保存数据
iTotal=GetTotalID();//得到链表中的记录数量
arr<<iTotal;
for each item in the list
{
for(int i=0;i<26;i++)
ar<<&(((BYTE*)pItem)+i);//写一个strPID中所有的数据
}
}
else
{//读数据
ar>>iTotal;
for(int i=0;i<iTotal;i++)
{
struct strPID *pID=new struct strPID;
for(int j=0;j<26;j++)
ar>>*(((BYTE*)pID)+j);//读一个strPID中所有的数据
//修改链表
}
}
}
</pre>
当然上面的代码很不完整,但已经可以说明问题。这样CAllPID就是一个可以支持序列化的类,并且可以根据记录的数量动态分配内存。在序列化中我们使用了CArchive类,该类用于在序列化时提供读写支持,它重载了<<和>>运算符号,并且提供Read和Write函数对数据进行读写。
</p>
<p>下面看看如何在文档中使用序列化功能,你只需要修改文档类的Serialize(CArchive& ar)函数,并调用各个进行序列化的类的Serial进行数据读写就可以了。当然你也可以在文档类的内部进行数据读写,下面的代码利用序列化功能读写数据:<pre>
class CYourDoc : public CDocument
{
void Serialize(CArchive& ar);
CString m_szDesc;
CAllPID m_allPID;
......
}
void CYourDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{//由于CString对CArchive定义了<<和>>操作符号,所以可以直接利用>>和<<
ar<<m_szDesc;
}
else
{
ar>>m_szDesc;
}
m_allPID.Serialize(ar);//调用数据类的序列化函数
}
</pre></p>
<p></p>
<p align=center><a href=teach_first.htm#charpter3>返回</a></p>
</td></tr>
</table>
<p align=center><small>版权所有 闻怡洋 <a href=http://vchelp.163.net/>http://vchelp.163.net/</a></small></p>
<!-- 耐特付费广告代码开始 不得修改. -->
<center><script language="javascript">
<!--
var date = new Date();
var ra = date.getTime() % 1000;
var ua = document.URL;
document.write("<iframe src='http://www.china-free.com/cgi-bin/ad/random.cgi?id=560' width=468 height=60 scrolling=no marginwidth=0 marginheight=0 frameborder=0 vspace=0 hspace=0 >");
document.write("<a href='http://www.china-free.com/cgi-bin/ad/random.cgi?id=560' target='_blank'>");
document.write("<img src='http://www.china-free.com/cgi-bin/ad/random.cgi?job=go&id=560' width=468 height=60 border=0></a>");
document.write("</iframe>");
//-->
</script>
<noscript>
<a target="_blank" href="http://www.china-free.com/cgi-bin/ad/random.cgi?id=560">
<img src="http://www.china-free.com/cgi-bin/ad/random.cgi?job=go&id=560" width="468" height="60" border="0"></a>
</noscript>
<br><a HREF="http://www.china-free.com/ad" target="_blank"><img SRC="http://www.china-free.com/ad/bd.gif" BORDER="0" ></a></center>
<!-- 耐特付费广告代码结尾. -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -