1、串行化:
void CArFileDlg::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{ // storing code
UpdateData(true);
ar<<m_add;
ar<<m_age;
ar<<m_name;
ar<<m_sex;
ar<<m_tel;
}
else
{ // loading code
ar>>m_add;
ar>>m_age;
ar>>m_name;
ar>>m_sex;
ar>>m_tel;
UpdateData(false);
}
}
2、联系CFile对象:
void CArFileDlg::OnSave()
{
// TODO: Add your control notification handler code here
static char sfile[]="自定义类型(*.mm)|*.mm";//文件过滤
CFileDialog dlg(FALSE,"mm","",NULL,sfile);//文件保存对话框对象
if(IDOK==dlg.DoModal())
{
CFile myfile(dlg.GetFileName(),CFile::modeCreate|CFile::modeReadWrite);
CArchive savefile(&myfile,CArchive::store);//CArchive对象
Serialize(savefile);
savefile.Close();
AfxMessageBox("保存成功!");
}
//CDialog::OnOK();
/*
UpdateData(true);
//对输入数据进行合法性检查
int len1=m_id.GetLength();
int len2=m_name.GetLength();
if(len1>10||len2>10||len1==0||len2==0)
{
AfxMessageBox("输入字符不大于10,且不能为空");
return;
}
char *p_id=new char[10]; //记录学号
char *p_name=new char[10]; //记录姓名
char *p_score=new char[3]; //记录成绩
memset(p_id,'\0',10);
memset(p_name,'\0',10);
memset(p_score,'\0',3);
_tcscpy(p_id,m_id);
_tcscpy(p_name,m_name);
itoa(m_score,p_score,10);
CFile file;
file.Open("test.txt",CFile::modeCreate|CFile::modeReadWrite);
file.SeekToEnd();
file.Write(p_id,10);
file.Write(p_name,10);
file.Write(p_score,3);
file.Close();
delete p_id;
delete p_name;
delete p_score;
AfxMessageBox("保存成功");
*/
}
void CArFileDlg::OnOpen()
{
// TODO: Add your control notification handler code here
static char sfile[]="自定义类型(*.mm)|*.mm";//文件过滤
CFileDialog dlg(TRUE,"mm","",NULL,sfile);//文件保存对话框对象
if(IDOK==dlg.DoModal())
{
CFile myfile(dlg.GetFileName(),CFile::modeReadWrite);
CArchive savefile(&myfile,CArchive::load);//CArchive对象
Serialize(savefile);
savefile.Close();
GetDlgItem(IDC_NAME)->EnableWindow(FALSE);
GetDlgItem(IDC_AGE)->EnableWindow(FALSE);
GetDlgItem(IDC_SEX)->EnableWindow(FALSE);
GetDlgItem(IDC_TEL)->EnableWindow(FALSE);
GetDlgItem(IDC_ADD)->EnableWindow(FALSE);
}
// CDialog::OnOK();
}