用CArchive类保存/打开文件
CArchive(
CFile* pFile,//对应的文件指针
INT nMode,// 模式,取值:CArchive::load, CArchive::store, CArchive::bNoFlushOnDelete
int nBufSize=4096, //默认buffer大小
void* lpBuf=NULL //如果默认则会自动释放该buffer所指内存,如果是手动指定则要手动释放内存。
);
【CArchive::load】
发读取文件时的Mode
【CArchive::store】
保存文件时的Mode
【 CArchive::bNoFlushOnDelete】
Prevents the archive from automatically callingFlushwhen the archive destructor is called. If you set this flag, you are responsible for explicitly callingClosebefore the destructor is called. If you do not, your data will be corrupted.
这个标识是用来避免调用archive对象的析构函数时自动调用Flush函数刷新数据,而如果设置了这个标识,那么就要手动显示在析构函数之前调用Close函数。
eg.
①存储文件
void OnWriteFile()
(
CFile file("1.txt", CFile::modeCreate | CFile::modeWrite);
CArchive ar(&file, CArchive::store);
int a = 2;
float b = 1.2f;
char ch = 'c';
CString str = "china";
ar<<a<<b<<ch<<str;
}
②读取文件
void OnReadFile()
{
CFile file("1.txt", CFile::modeRead);
CArchive ar(&file, CArchive::load);
int a = 0;
float b = 0.0f;
char ch;
CString str = "";
ar>>a>>b>>ch>>str;
}
NOTE:对同一个文件读和写的顺序要一致。