VC++之文件基本操作之基于CArchive类的文件读写

来源:互联网 发布:js同源 编辑:程序博客网 时间:2024/05/10 17:59

一、创建单文档应用程序

二、添加菜单资源

菜单ID与标题
                    文件操作
IDM_DU     读取文件
IDM_XIE    写入文件

三、添加函数


四、添加代码(红色部分)

void CCDocView::OnDu()
{
// TODO: Add your command handler code here
CFile file("hello.txt",CFile::modeRead);
CArchive arv(&file,CArchive::load);
int age;
CString str,str2;
arv>>str>>age;
    str2.Format("%s %d",str,age);
MessageBox(str2);
}

void CCDocView::OnXie()
{
// TODO: Add your command handler code here
CFile file("hello.txt",CFile::modeCreate|CFile::modeWrite);         //创建文件
CArchive arv(&file,CArchive::store);
CString str("My age is");
int age=22;
arv<<str<<age;
}

五、编译

六、运行


七、函数说明

       CArchive::CArchive函数声明

        CArchive(CFile* pFile,UINT nMode,int nBufSize=4096,void* lpBuf=NULL)

       pFile:指向CFile类对象的指针。

       nMode:对象加载或保存识别。

       nBufSize:文件大小,默认值为“4096”。

       功能:CArchive类对象的构造函数,同时说明该函数是用于加载对象还是保存对象。

0 0
原创粉丝点击