一、创建单文档应用程序
二、添加菜单资源
菜单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类对象的构造函数,同时说明该函数是用于加载对象还是保存对象。