【上位机——MFC】序列化机制

相关类

CFile-文件操作类,封装了关于文件读写等操作

CFile::Open

CFile::Write/Read

CFile::Close

CFile::SeekToBegin / SeekToEnd / Seek

代码示例

cpp 复制代码
#include <afxwin.h>
#include <iostream>

using namespace std;

void File() {
	CFile file;
	file.Open("E:/MFC/MFCDay02/file.txt",CFile::modeCreate | CFile::modeReadWrite);
	char str[] = "hello file";

	file.Write(str,strlen(str));
	file.SeekToBegin();

	char buf[256] = { 0 };
	long nLen = file.Read(buf,256);
	cout << buf << ' ' << nLen << endl;

}

int main() {
	File();

	return 0;
}

序列化机制的作用

以二进制流形式读写硬盘文件,但效率很高。

CFile文件操作类,完成硬盘文件的读写操作。

CArchive-归档类,完成内存数据的读写操作。

序列化变量

序列化机制的使用

  1. 创建或打开文件 CFile::Open
  2. 定义归档类对象 CArchive ar
  3. 数据序列化 ar << 数据
  4. 关闭归档类对象 ar.Close()
  5. 关闭文件 CFile::Close();

反序列化机制的使用

  1. 打开文件 CFile::Open
  2. 定义归档类对象 CArchive ar
  3. 数据反序列化 ar >> 变量
  4. 关闭归档类对象 ar.Close()
  5. 关闭文件 CFile::Close();

代码示例

cpp 复制代码
#include <afxwin.h>
#include <iostream>

using namespace std;

//序列化
void Store() {
	CFile file;
	file.Open("E:/MFC/MFCDay02/serial.txt", CFile::modeCreate | CFile::modeWrite);

	CArchive ar(&file, CArchive::store,4096);
	long age = 18;
	ar << age;
	float score = 88.5;
	ar << score;
	CString name = "zhangsan";
	ar << name;
	ar.Close();
	file.Close();

}

//反序列化
void Load() {
	CFile file;
	file.Open("E:/MFC/MFCDay02/serial.txt", CFile::modeRead);
	CArchive ar(&file, CArchive::load, 4096);
	long age;
	ar >> age;
	float score;
	ar >> score;
	CString name;
	ar >> name;

	cout << "age " << age << " score " << score <<" name "<< name << endl;

}


int main() {

	//Store();

	Load();

	return 0;
}

序列化对象的使用

  1. 类必须派生自CObject
  2. 类内必须添加声明宏 DECLARE_SERIAL(theClass)
  3. 类外必须添加实现宏 IMPLEMENT_SERIAL(theClass,baseClass,1)
  4. 类必须重写虚函数Serialize
    当类具备上述三个要件后,类对象就可以序列化到文件中保存了。
cpp 复制代码
#include <afxwin.h>
#include <iostream>

using namespace std;

//1.类必须派生自CObject
class CMyDoc :public CDocument {
	//2.类内必须添加声明宏 DECLARE_SERIAL(theClass)
	DECLARE_SERIAL(CMyDoc)

public:
	CMyDoc(int age = 0, float score = 0.0, CString name = "") :
		m_age(age), m_score(score), m_name(name) {
	};
	
	int m_age;
	float m_score;
	CString m_name;
	//4.类必须重写虚函数Serialize
	virtual void Serialize(CArchive& ar);

};
//3.类外必须添加实现宏 IMPLEMENT_SERIAL(theClass,baseClass,1)
IMPLEMENT_SERIAL(CMyDoc,CDocument,1)

void CMyDoc::Serialize(CArchive& ar) {
	if (ar.IsStoring()) {
		ar << m_age << m_score << m_name;
	}
	else {
		ar >> m_age >> m_score >> m_name;
	}
}


void Store() {

	CFile file;
	file.Open("E:/MFC/MFCDay02/serial.txt", CFile::modeCreate | CFile::modeWrite);

	CArchive ar(&file, CArchive::store, 4096);
	CMyDoc data(18, 88.5, "zhangsan");
	ar << &data;
	ar.Close();
	file.Close();


}

void Load() {

	CFile file;
	file.Open("E:/MFC/MFCDay02/serial.txt", CFile::modeRead);
	CArchive ar(&file, CArchive::load, 4096);

	CMyDoc* pdata = NULL;
	ar >> pdata;
	ar.Close();
	file.Close();
	cout << pdata->m_age <<' ' << pdata->m_score << ' ' << pdata->m_name << endl;
}


int main() {

	//Store();

	Load();

	return 0;
}
相关推荐
春日见15 分钟前
E2E自驾规控30讲:导论
开发语言·驱动开发·git·matlab·计算机外设
wangchunting16 分钟前
Jvm-垃圾收集器
java·开发语言·jvm
沐知全栈开发27 分钟前
PHP Math: 精通PHP中的数学函数与应用
开发语言
吴声子夜歌39 分钟前
JavaScript——call()、apply()和bind()
开发语言·前端·javascript
平凡灵感码头41 分钟前
C语言 printf 数据打印格式速查表
c语言·开发语言·算法
hz_zhangrl1 小时前
CCF-GESP 等级考试 2026年3月认证C++三级真题解析
c++·算法·程序设计·gesp·gesp2026年3月·gesp c++三级
兮℡檬,1 小时前
答题卡识别判卷
开发语言·python·计算机视觉
酉鬼女又兒2 小时前
零基础快速入门前端DOM 操作核心知识与实战解析(完整汇总版)(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·javascript·职场和发展·蓝桥杯·js
kyle~2 小时前
C++----函数指针与函数指针类型 返回值类型 (*类型名)(参数列表)
开发语言·c++
努力中的编程者2 小时前
二叉树(C语言底层实现)
c语言·开发语言·数据结构·c++·算法