zoukankan      html  css  js  c++  java
  • MFC笔记6

    1.MFC文件的读写操作

    写操作

    创建一个编辑框(IDC_INFOR_EDIT1),在里面输入信息,创建一个按钮(IDC_BUTTON),点击按钮会触发(OnBnClickedButton2()函数)将编辑框信息保存在文件(abx.txt)中

    void CShowLanguage::OnBnClickedButton2()
    {
        // TODO: 在此添加控件通知处理程序代码
        //得到当前时间
        CTime time;
        time=CTime::GetCurrentTime();
        //将当前时间转换成标准时间
        CString data=time.Format(_T("%Y-%m-%d %H:%M:%S %W-%A"));
        
        CString strText(_T(""));
        GetDlgItemText(IDC_INFOR_EDIT1,strText);//获取edit中的数据
        int i;
        try
        {
            CStdioFile file;
            //打开文件,文件不存在就创建
            i=file.Open(_T("abx.txt"),CFile::modeCreate|CFile::modeReadWrite);
            file.SeekToEnd();
            CArchive ar(&file,CArchive::store);
            ar.WriteString(str);
            ar.WriteString(strText);
            //回车换行
            ar.WriteString(_T("
    "));
            ar.WriteString(data);
            ar.Close();
            file.Close();
            MessageBox(_T("文件存储成功"),MB_OK);
        }catch(CFileException * e)
        {
            e->ReportError();
            e->Delete();
        }
        
    }

    读操作

    将文件(abx.txt)中信息读到编辑框(IDC_SHOW_EDIT1);

        CStdioFile file;
        CFileException e;
        file.Open(_T("abx.txt"),CFile::modeRead,&e);
        CString str2;
        file.ReadString(str2);
        SetDlgItemText(IDC_SHOW_EDIT1,str2);
  • 相关阅读:
    自然拼读
    windws蓝屏解决方案
    chrome
    ubuntu安装英伟达驱动
    ubuntu基础
    kvm(未完成2021-04-26)
    istio
    OpenSSH
    su 与 su -关系
    read命令/ declare/set
  • 原文地址:https://www.cnblogs.com/zhangerxiaoma/p/5000537.html
Copyright © 2011-2022 走看看