标签:blog 使用 os io 文件 数据 for 2014
一 MFC的文件操作}
程序示例:
新建win32控制台程序 ,添加afxwin.h头文件,设置包含MFC动态库
主程序代码如下:
// MFCfile.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <AFXWIN.H>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//打印文件状态信息
void show_status(CFileStatus& status)
{
printf("文件名:%s\n",status.m_szFullName);
printf("大小:%g\n",status.m_size);
}
//打印空格 ,为了清楚地显示目录结构
void printspace(int n)
{
if(n>0)
{
for(int i=0;i<n;i++) printf(" ");
}
}
//文件读取和写入
void CFileTest()
{
CFile file;//定义CFile对象
//打开
CString filename="MFCfile.txt";
BOOL bresult=file.Open(filename,
CFile::modeCreate|CFile::modeReadWrite);
if(!bresult) return;//打开失败,直接返回
try
{
CString str="Hello my cfile!";
file.Write(str,str.GetLength());
printf("写入成功\n");
char sztxt[100]={0};
file.SeekToBegin();//将文件指针移动到文件开头
file.Read(sztxt,100);
printf("读取成功,字符串:%s\n",sztxt);
file.Close();
CFileStatus status;
//如果其他程序正在使用该文件,有可能出CFileException异常
CFile::GetStatus(filename,status);//获取状态信息
show_status(status);
CTimeSpan span(7,0,0,0);//时间间隔,7天
status.m_ctime-=span;//修改创建时间提前7天
CFile::SetStatus(filename,status);//设置状态信息
}
catch (CMemoryException* e)
{
printf("内存出错\n");
}
catch (CFileException* e)
{
printf("文件操作出错\n");
}
catch (CException* e)
{
printf("其他错误\n");
}
}
//文件查找
void CFileFindTest(CString strPath,int space=0)
{
CFileFind cf;
BOOL result=cf.FindFile(strPath+"\\*.*");//目录后要加通配符
while(result)
{
result=cf.FindNextFile();
CString filename=cf.GetFileName();
printspace(space);
if(cf.IsDirectory())
{
if(!cf.IsDots())
{
CString filepath=cf.GetFilePath();
//printf(" subdir:%s\n",filepath);
CFileFindTest(filepath,space+1);
}
printf("目录名:%s\n",filename);
}
else printf("文件名:%s\n",filename);
}
cf.Close();
}
//序列化(二进制),写入数据
void CArchiveStoreTest()
{
CFile file;//定义CFile对象
//打开
CString filename="MFCfile1.txt";
BOOL bresult=file.Open(filename,
CFile::modeCreate|CFile::modeWrite);
if(!bresult) return;//打开失败,直接返回
CArchive ar(&file,CArchive::store);
ar<<100<<12.46<<"hello archive!";//写入
ar.Close();
file.Close();
}
//序列化(二进制),读取数据
void CArchiveloadTest()
{
CFile file;//定义CFile对象
//打开
CString filename="MFCfile1.txt";
BOOL bresult=file.Open(filename,
CFile::modeNoTruncate|CFile::modeRead);
if(!bresult) return;//打开失败,直接返回
CArchive ar(&file,CArchive::load);
int i;
double d;
CString str;
ar>>i>>d>>str;//读取
ar.Close();
file.Close();
printf("%d,%f,%s\n",i,d,str);
}
//1 定义支持序列化的类
class CStudent:public CObject
{
public:
virtual void Serialize( CArchive& ar );//重写虚函数
CStudent(){}
CStudent(CString strName,UINT nAge)
{
m_strName=strName;
m_nAge=nAge;
}
void Show()
{
printf("姓名:%s\n",m_strName);
printf("年龄:%d\n",m_nAge);
}
public:
CString m_strName;
UINT m_nAge;
//序列化的宏
DECLARE_SERIAL(CStudent)
/* //宏内容
_DECLARE_DYNCREATE(CStudent)
AFX_API friend CArchive& AFXAPI
operator>>(CArchive& ar, CStudent* &pOb);
*/
};
IMPLEMENT_SERIAL(CStudent,CObject,1)
/* //宏内容
CObject* PASCAL CStudent::CreateObject()
{
return new CStudent;
}
_IMPLEMENT_RUNTIMECLASS(CStudent, CObject, 1,
CStudent::CreateObject)
AFX_CLASSINIT _init_CStudent(RUNTIME_CLASS(CStudent));
CArchive& AFXAPI operator>>(CArchive& ar, CStudent* &pOb)
{
pOb = (CStudent*) ar.ReadObject(RUNTIME_CLASS(CStudent));
return ar;
}
*/
void CStudent::Serialize( CArchive& ar )
{
//首先调用父类的序列化函数
CObject::Serialize( ar );
if (ar.IsStoring())//存储操作
{
ar<<m_strName<<m_nAge;
}
else//加载操作
{
ar>>m_strName>>m_nAge;
}
}
//存入
void ObjectStore(CStudent & stu)
{
CFile file;
file.Open("C:\\stu.dat",
CFile::modeCreate|CFile::modeWrite);
CArchive ar(&file,CArchive::store);
ar<<&stu;
ar.Close();
file.Close();
}
//读出
void ObjectLoad()
{
CFile file;
file.Open("C:\\stu.dat",CFile::modeRead);
CArchive ar(&file,CArchive::load);
CStudent *pStu=NULL;
ar>>pStu;
ar.Close();
file.Close();
if (pStu)
{
pStu->Show();
}
}
int main(int argc, char* argv[])
{
//CFileTest();
//CFileFindTest("../../");
CArchiveStoreTest();
CArchiveloadTest();
CStudent stu("student1",23);
ObjectStore(stu);
ObjectLoad();
getchar();
return 0;
}
C++MFC编程笔记day07 MFC的文件操作、序列化和保存,布布扣,bubuko.com
C++MFC编程笔记day07 MFC的文件操作、序列化和保存
标签:blog 使用 os io 文件 数据 for 2014
原文地址:http://blog.csdn.net/pukuimin1226/article/details/38367549