VC: file serialization (CFileDialog class, CFile class, CArchive class, and Edit control)

Source: Internet
Author: User

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 ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.