1 概要
1.(源码)
MFC提供了CFile类方便文件的读写,首先要知道,文件的数据读取、数据写入与文件指针的操作都是以字节为单位的,数据的读取和写入都是从文件指针的位置开始的,当打开一个文件的时候,文件指针总是在文件的开头 。常规方法如下:
CFile file;file.open( LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL );
//是文件名,可包含文件路径,若只有文件名,则默认路径为工程路径,是文件打开模式,是打开失败时用来接收失败信息,一般设置为NULL 。
1.2
当我们创建对象的时候,对象就存在于内存中,当其生命周期结束后,这些被创建的对象就要被销毁;当对象被销毁后,我们就无法知道这些对象的值 。MFC提供了类可以将对象数据保存到永久设备,比如磁盘文件 。当应用程序重新启动后,类可以帮助我们从磁盘文件读取这些数据,然后在内存中重新构建对应的对象;这样就使得我们的对象数据永久存在,该过程称之为序列化(或者串行化) 。
2 代码
2.1摘要
double g_data1 = 0.0;double g_data2 = 0.0;double g_data3 = 0.0;int g_ndata = http://www.kingceram.com/post/0;void CCFileTestDlg::OnButton1() {// TODO: Add your control notification handler code hereCString data1,data2,data3,data4;CFile SetFile;if(SetFile.Open(_T("setfujiandata.ini"),CFile::modeRead)){ CArchive ar(&SetFile,CArchive::load);try{ar >> data1;ar >> data2;ar >> data3;ar >> data4;}catch(CArchiveException* e){e->ReportError();e->Delete();ar.Close();SetFile.Close();}ar.Close();SetFile.Close();g_data1 = atof(data1);g_data2 = atof(data2);g_data3 = atof(data3);g_ndata = http://www.kingceram.com/post/atoi(data4);SetDlgItemText(IDC_EDIT1,data1);SetDlgItemText(IDC_EDIT2,data2);SetDlgItemText(IDC_EDIT3,data3);SetDlgItemText(IDC_EDIT4,data4);}}void CCFileTestDlg::OnButton2() {// TODO: Add your control notification handler code here// TODO: 在此添加控件通知处理程序代码CString data1,data2,data3,data4;GetDlgItemText(IDC_EDIT1,data1);GetDlgItemText(IDC_EDIT2,data2);GetDlgItemText(IDC_EDIT3,data3);GetDlgItemText(IDC_EDIT4,data4);CFile SetFile;if(SetFile.Open(_T("setfujiandata.ini"),CFile::modeCreate|CFile::modeWrite)){ CArchive ar(&SetFile,CArchive::store);try{ar << data1;ar << data2;ar << data3;ar << data4;}catch(CArchiveException* e){e->ReportError();e->Delete();ar.Close();SetFile.Close();}g_data1 = atof(data1);g_data2 = atof(data2);g_data3 = atof(data3);g_ndata = http://www.kingceram.com/post/atof(data4);ar.Close();SetFile.Close();//g_bSet = true;MessageBox(_T("保存成功!"));}else{MessageBox(_T("保存失败!"));}}void CCFileTestDlg::OnButton4() {// TODO: Add your control notification handler code hereSetDlgItemText(IDC_EDIT1,"");SetDlgItemText(IDC_EDIT2,"");SetDlgItemText(IDC_EDIT3,"");SetDlgItemText(IDC_EDIT4,"");}
2.2 代码
// CFileTestDlg.cpp : implementation file//#include "stdafx.h"#include "CFileTest.h"#include "CFileTestDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog{public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX);// DDX/DDV support//}}AFX_VIRTUAL// Implementationprotected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT}void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAPEND_MESSAGE_MAP()/// CCFileTestDlg dialogCCFileTestDlg::CCFileTestDlg(CWnd* pParent /*=NULL*/): CDialog(CCFileTestDlg::IDD, pParent){//{{AFX_DATA_INIT(CCFileTestDlg)// NOTE: the ClassWizard will add member initialization here//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CCFileTestDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CCFileTestDlg)// NOTE: the ClassWizard will add DDX and DDV calls here//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CCFileTestDlg, CDialog)//{{AFX_MSG_MAP(CCFileTestDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON1, OnButton1)ON_BN_CLICKED(IDC_BUTTON2, OnButton2)ON_BN_CLICKED(IDC_BUTTON4, OnButton4)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/// CCFileTestDlg message handlersBOOL CCFileTestDlg::OnInitDialog(){CDialog::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX
- CFile使用主要方法答疑
- C/C++内存泄漏原因分析与应对方法
- c++中在头文件中定义static变量
- 分割一切模型 Fast SAM C++推理部署
- 数据结构 C++实现 基于不同策略的英文单词的词频统计和检索系统
- android framework 系统native c/c++源码查看跳转工具
- 高效学 C++|编程实例之计算器
- 自己写的俄罗斯方块
- 三 使用C++和Qt的涂鸦程序
- C++ 编译器优化与SIMD指令集