zoukankan      html  css  js  c++  java
  • VC:文件串行化(CFileDialog类、CFile类、CArchive类、Edit控件)

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

    }

  • 相关阅读:
    Linux-exec族函数
    Linux-竟态初步引入
    Linux-waitpid介绍
    Java基础:Java运算符:算术运算符
    Java中的算术运算符
    JAVA冒泡排序
    引用 java的一些基本概念
    Tomcat服务器的下载安装跟基本配置
    Tomcat配置Web站点
    Tomcat+JSP经典配置实例
  • 原文地址:https://www.cnblogs.com/shenchao/p/2725220.html
Copyright © 2011-2022 走看看