以下就实例来说:
首先建立一个类,原理不讲了,单就过程方式操作介绍一下.
比如我们建立一个类CPrintFile::Public CObject
然后在类中加入宏定义DECLARE_SERIAL(CPrintFile)
然后加入函数void Serialize(CArchive & ar);
CString name[1000];
CString contact[1000];
CString address[1000];
CString mail[1000];
CString stime[1000];
CString etime[1000];
CString self_address[1000];
int all;
在cpp文件中加入
IMPLEMENT_SERIAL(CPrintFile,CObject,1)
void CPrintFile::Serialize(CArchive &ar)
{
int i;
if(ar.IsLoading ())
{
for(i=0;i<1000;i++)
ar >> name[i] >>contact[i]>>address[i]>>mail[i]>>stime[i]>>etime[i]>>self_address[i];
ar>>all;
}else{
for(i=0;i<1000;i++)
ar << name[i]<
ar<}
}
/////
串行化只能对具体的类型进行,不能对类,数组,结构体等进行操作,所以我们一般都是建立这种数组的
在目标类中我们这样操作
读入
//第一步将数据存储到临时文件
CPrintFile* me=new CPrintFile;
int i;
for(i=0;i {
me->name[i]=m_list.GetItemText(m_sel[i],3);
me->contact[i]=m_list.GetItemText(m_sel[i],4);
me->address[i]=m_list.GetItemText(m_sel[i],6);
me->mail[i]=m_list.GetItemText(m_sel[i],7);
me->stime[i]=m_list.GetItemText(m_sel[i],10);
me->etime[i]=m_list.GetItemText(m_sel[i],11);
me->self_address[i]=m_list.GetItemText(m_sel[i],15);
}
me->all=sel_count;
////
CFile pFile;
CFileException fe;
if(!pFile.Open("printlist.tmp",CFile::modeCreate | CFile::modeReadWrite | CFile::shareExclusive,&fe))
{
fe.ReportError();
return;
}
CArchive saveArchive(&pFile,CArchive::store|CArchive::bNoFlushOnDelete);
me->Serialize(saveArchive);
saveArchive.Flush();
saveArchive.Close();
////
delete me;
读出
CFileException fe;
CFile pFile;
pFile.Open("printlist.tmp",CFile::modeRead|CFile::shareDenyWrite,&fe);
CArchive loadArchive(&pFile, CArchive::load | CArchive::bNoFlushOnDelete);
me->Serialize(loadArchive);
loadArchive.Flush();
loadArchive.Close();
阅读(1168) | 评论(0) | 转发(0) |