现在的位置: 首页 > 综合 > 正文

CArchive类的使用

2013年10月05日 ⁄ 综合 ⁄ 共 3225字 ⁄ 字号 评论关闭
VC中利用CArchive类存取数据
Demo1:
1、MSDN关于CArchive的翻译
The CArchive class allows you to save a complex network of objects in a permanent binary form (usually disk storage) that persists after those objects are deleted. Later you can load the objects from persistent storage, reconstituting them in memory. This process of making data persistent is called “serialization.”

CArchive 类允许在永久的二进制形式上(通常为磁盘存储器)保存复杂的对象网络,并在对象被删除后(对象的生命周期结束被销毁)保持持久性。之后,你可以从持久的存储器上加载这些对象并可在内存中重新构建他们。使数据持久性的过程叫“串行化”

You can think of an archive object as a kind of binary stream. Like an input/output stream, an archive is associated with a file and permits the buffered writing and reading of data to and from storage. An input/output stream processes sequences of ASCII characters, but an archive processes binary object data in an efficient, nonredundant format.

你可以把archive对象认为是一种类型的二进制流。像一个输入输出流,一个archive和一个文件相关,允许带缓冲地写入/读取数据到/从存储器上。一个输入/输出流处理ASCII字符序列,但是一个archive以一种效率的,非冗余的格式处理二进制对象数据。

You must create a CFile object before you can create a CArchive object. In addition, you must ensure that the archive’s load/store status is compatible with the file’s open mode. You are limited to one active archive per file.

在创建CArchive对象之前,必须创建一个CFile对象。另外,必须确保archive创建对象的加载/保存对象的状态和文件打开的方式相一致。你受限于每个活动的archive对应一个文件。

When you construct a CArchive object, you attach it to an object of class CFile (or a derived class) that represents an open file. You also specify whether the archive will be used for loading or storing. A CArchive object can process not only primitive types but also objects of CObject-derived classes designed for serialization. A serializable class usually has a Serialize member function, and it usually uses the DECLARE_SERIAL and IMPLEMENT_SERIAL macros, as described under class CObject.

当你构建一个CArchive对象时,将其与CFile类(或其派生类)的对象关联在一起,表示一个打开的文件。你也可以指定archive是否被用来加载或保存。一个CArchive对象不仅仅可以处理基本类型,也可以处理CObject类的派生对象(使其串行化)。

The overloaded extraction (>>) and insertion (<<) operators are convenient archive programming interfaces that support both primitive types and CObject-derived classes.

重载的提取(>>)和插入(<<)操作符,是方便的存储程序接口。支持基本类型和CObject的派生类。

2、利用CArchive实现文件的存取:

写入文件:
    CFile file("1.txt",CFile::modeCreate | CFile::modeWrite);
    CArchive ar(&file,CArchive::store);
    int i=333;
    float f=1.3333f;
    char ch='v';
    ar<<i<<f<<ch;

读取文件:
    CFile file("1.txt",CFile::modeRead);
    CArchive ar(&file,CArchive::load);
    int i;
    float f;
    char ch;
    CString strResult;
    ar>>i>>f>>ch;
    strResult.Format("%d,%f,%c",i,f,ch);//格式化输出数据
    MessageBox(strResult);//显示读取数据

 
 
DEMO2:
 
例如用Dialog-Based应用程序来实现文件读写:
void CMyDialog::SaveFile()
{
CFile myfile("filename",CFile::modeCreate | CFile::modeReadWrite); 
CArchive savefile(&myfile,CArchive::store);
Serialize(savefile);
savefile.Close();
}
 
void CMyDialog::OpenFile()
{
CFile myfile("filename",CFile::modeReadWrite);
CArchive openfile(&myfile,CArchive::load);
Serialize(openfile);
openfile.Close();
}
 
同时要将虚函数Serialize() 加上:
void CMyDialog::Serialize(CArchive& ar)
{
 if (ar.IsStoring())
 { // storing code
//假如CMyDialog有一成员CString mystring,将mystring 写入文件:
ar<<mystring;
 }
 else
 { // loading code
//读出数据:
ar>>mystring;
 }
//文件的读写是在Serialize()中完成的
}
当然也可向下调用成员变量的Serialize()函数 那么需要先像这样在成员变量的Serialize函数中添加代码
而此成员变量须是从CObject继承而来,因为CObject才能Serialize
 
 
 
【上篇】
【下篇】

抱歉!评论已关闭.