1. serialization:
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. Contact the CFile object:
Void CArFileDlg: OnSave ()
{
// TODO: Add your control notification handler code here
Static char sfile [] = "custom type (*. mm) | *. mm"; // file filtering
CFileDialog dlg (FALSE, "mm", "", NULL, sfile); // object of the File Save dialog box
If (IDOK = dlg. DoModal ())
{
CFile myfile (dlg. GetFileName (), CFile: modeCreate | CFile: modeReadWrite );
CArchive savefile (& myfile, CArchive: store); // CArchive object
Serialize (savefile );
Savefile. Close ();
AfxMessageBox ("saved successfully! ");
}
// CDialog: OnOK ();
/*
UpdateData (true );
// Check the validity of input data
Int len1 = m_id.GetLength ();
Int len2 = m_name.GetLength ();
If (len1> 10 | len2> 10 | len1 = 0 | len2 = 0)
{
AfxMessageBox ("the input character cannot exceed 10 and cannot be blank ");
Return;
}
Char * p_id = new char [10]; // record student ID
Char * p_name = new char [10]; // Record Name
Char * p_score = new char [3]; // record the score
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 ("saved successfully ");
*/
}
Void CArFileDlg: OnOpen ()
{
// TODO: Add your control notification handler code here
Static char sfile [] = "custom type (*. mm) | *. mm"; // file filtering
CFileDialog dlg (TRUE, "mm", "", NULL, sfile); // object of the File Save dialog box
If (IDOK = dlg. DoModal ())
{
CFile myfile (dlg. GetFileName (), CFile: modeReadWrite );
CArchive savefile (& myfile, CArchive: load); // CArchive object
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 ();
}