当前位置: 首页 > news >正文

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

相关类

CFile-文件操作类,封装了关于文件读写等操作
CFile::Open
CFile::Write/Read
CFile::Close
CFile::SeekToBegin / SeekToEnd / Seek

代码示例

#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();

代码示例

#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
    当类具备上述三个要件后,类对象就可以序列化到文件中保存了。
#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.类必须重写虚函数Serializevirtual 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;
}

相关文章:

  • 机器学习在信用卡欺诈检测中的应用思考
  • 基于英特尔 RealSense D455 结构光相机实现裂缝尺寸以及深度测量
  • svn服务器迁移
  • 使用VSCode在Windows 11上编译运行项目
  • Mybatis标签使用 -association 绑定对象,collection 绑定集合
  • 【背包dp----01背包】例题三------(标准的01背包+变种01背包1【恰好装满背包体积 产生的 最大价值】)
  • 【ROS2】Nav2源码之行为树定义、创建、加载
  • 【论文阅读】Attentive Collaborative Filtering:
  • 缓存替换算法与存储器管理的分页、分段、段页式管理联系
  • 培训机构用的教务系统
  • 如何延长电脑使用寿命?
  • 神经网络在模式识别中的应用:从语音到视觉的智能解析
  • react中的用法——setDisabled dva dispatch effects
  • 更新编译器到ARM compiler6,很多报错问题
  • FPGA 不兼容故障及处理
  • 人工智能数据标注服务规范
  • SSA-CNN+NSGAII+熵权TOPSIS,附相关气泡图!
  • Excel点击单元格内容消失
  • Spark应用部署模式实例
  • 力扣刷题Day 37:LRU 缓存(146)
  • 刘元春在《光明日报》撰文:以法治护航民营经济高质量发展
  • 印度军方否认S-400防空系统被摧毁
  • 时代中国控股:前4个月销售额18.1亿元,境外债重组协议押后聆讯至5月底
  • 巴基斯坦信德省卡拉奇发生爆炸
  • 鸿蒙电脑正式亮相,五年布局积累超2700项核心专利
  • 中国难以承受高关税压力?外交部:任何外部冲击都改变不了中国经济基本面