以二进制流形式读写硬盘文件,但这种方式效率高。
CFile:
void fun() { CFile file; file.Open("C:/path/to/file", CFile::modeReadWrite | CFile::modeCreate); char str[] = "hello file"; file.Write(str, strlen(str)); file.SeekToBegin(); char buf[256] = { 0 }; long nLen = file.Read(buf, 255); std::cout << buf << " : " << nLen << std::endl; }
序列化机制使用
在内存中使用 \0 标识字符串的结束;在序列化的文件中,在字符串的前面用一个字节加上长度
图中的特殊符号就保存了字符串的长度
void store() { CFile file; file.Open("C:/file", CFile::modeReadWrite); CArchive ar(&file, CArchive::store); ar << 1L << 88.5 << "string"; ar.Close(); file.Close(); } void load() { CFile file; file.Open("C:/file", CFile::modeReadWrite); CArchive ar(&file, CArchive::load); long a; float b; CString c; ar >> a >> b >> c; std::cout << a << b << c << std::endl; ar.Close(); file.Close(); }
序列化过程:
反序列化过程:
序列化对象就是将对象的各个成员变量序列化,但与直接序列化各个字段不同,还会保存类名、类大小、类版本,然后保存字段。
要求:
CObject
DECLARE_SERIAL(theClass)
IMPLEMENT_SERIAL(theClass, baseClass, 1)
Serialize
class CMyDoc : public CDocument { DECLARE_SERIAL(CMyDoc) // 包含了 DECLARE_DYNCREATE(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; virtual void Serialize(CArchive& ar); }; IMPLEMENT_SERIAL(CMyDoc, CDocument, 1) // 包含了 IMPLEMENT_DYNCREATE(CMyDoc, CDocument) 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("C:/file", CFile::modeReadWrite); CArchive ar(&file, CArchive::store); CMyDoc data(1, 5.5, "zhangsan"); ar << &data; // 序列化对象就是将对象的各个成员变量序列化 ar.Close(); file.Close(); } void load() { CFile file; file.Open("C:/file", CFile::modeReadWrite); CArchive ar(&file, CArchive::load); CMyDoc* pData = NULL; ar >> pData; ar.Close(); file.Close(); std::cout << pData->m_age << pData->m_score << pData->m_name << std::endl; }
无模式对话框架构使用 CDialog CWinApp
CDialog
或 CDialogEx
均可// 这份代码将对话框做为主窗口,点击“确定”按钮后窗口消失但进程依然存在,因为父类中虚函数使用了 EndWindow 隐藏窗口 #include <afxwin.h> #include "resource1.h" class CMyDlg : public CDialog { DECLARE_MESSAGE_MAP() public: void OnCancel(); }; BEGIN_MESSAGE_MAP(CMyDlg, CDialog) ON_COMMAND(IDCANCEL, OnCancel) END_MESSAGE_MAP() void CMyDlg::OnCancel() { this->DestroyWindow(); } class CMyWinApp : public CWinApp { public: virtual BOOL InitInstance(); }; BOOL CMyWinApp::InitInstance() { CMyDlg* pdlg = new CMyDlg; pdlg->Create(IDD_DIALOG1); m_pMainWnd = pdlg; pdlg->ShowWindow(SW_SHOW); pdlg->UpdateWindow(); return TRUE; } CMyWinApp theApp;
执行过程:
模式对话框
#include <afxwin.h> #include "resource1.h" class CMyDlg : public CDialog { DECLARE_MESSAGE_MAP() public: CMyDlg() : CDialog(IDD_DIALOG1) {} void OnCancel(); }; BEGIN_MESSAGE_MAP(CMyDlg, CDialog) ON_COMMAND(IDCANCEL, OnCancel) END_MESSAGE_MAP() void CMyDlg::OnCancel() { this->DestroyWindow(); } class CMyWinApp : public CWinApp { public: virtual BOOL InitInstance(); }; BOOL CMyWinApp::InitInstance() { CMyDlg* pdlg = new CMyDlg; this->m_pMainWnd = pdlg; //pdlg->Create(IDD_DIALOG1); //pdlg->ShowWindow(SW_SHOW); //pdlg->UpdateWindow(); pdlg->DoModal(); return FALSE; // 不再执行MFC库中安排的Run函数 } CMyWinApp theApp;