MFC读写文件实例

程序功能:点击写入文件按钮将输入编辑框中内容写入以系统时间命名的文件中,点击读取文件按钮将选中的文件内容显示到静态文本控件中。

相关代码如下:

cpp 复制代码
void CWR_FILEDlg::OnButton1() 
{
		CString str;
   GetDlgItem(IDC_EDIT1)->GetWindowText(str);
   nLength=str.GetLength();

   sz=new char[nLength];
   sz=str.GetBuffer(0);
   CString strFileName;
	strFileName.Format("%s%s.MBD",m_strCurDataPath,
		CTime::GetCurrentTime().Format("%Y%m%d%H%M%S"));
		CFile file;
		file.Open(strFileName,CFile::modeCreate|CFile::modeWrite);   
		char buf[64];
		sprintf(buf,"%s",sz);
	    file.Write(buf,sizeof(buf));
    tm = CTime::GetCurrentTime(); 
    file.Write(&tm,sizeof(tm));
		file.Close();	
}

void CWR_FILEDlg::OnButton2() 
{
	CString strFileName;
	 CFileDialog dlg(TRUE,"",strFileName,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		"数据文件(*.MBD)|*.MBD||");
	if(dlg.DoModal() == IDOK)
	{		
	    m_strFileName = dlg.GetPathName();
		CFile file;
	 	file.Open(m_strFileName,CFile::modeRead);
	    char buf[64];
        file.Read(buf,sizeof(buf));
	    file.Read(&tm,sizeof(tm));
		file.Close();	
	   	CString	str =tm.Format("%Y-%m-%d %H:%M:%S");	
		SetDlgItemText(IDC_STATIC1,buf);
		SetDlgItemText(IDC_STATIC2,str);
	
}
}
相关推荐
橙子也要努力变强5 小时前
信号的保存、阻塞与递达
linux·服务器·c++
旖-旎5 小时前
深搜练习(组合总和)(7)
c++·算法·深度优先·力扣
T0uken5 小时前
基于 vcpkg 与 LLVM-MinGW 的 Qt6 静态链接开发方案
c++·windows·qt
睡一觉就好了。6 小时前
C++11(一)
c++
水云桐程序员6 小时前
C++的主要应用场景
c++·学习方法
叼烟扛炮7 小时前
C++第一讲:C++ 入门基础
开发语言·c++·函数重载·引用·内联函数·nullptr
zh_xuan7 小时前
使用libcurl调用http接口
c++·github·libcurl
zh路西法7 小时前
【RDKX5多摄像头模型推理】USB带宽限制与ROS2话题零拷贝转发
linux·c++·python·深度学习
千寻girling8 小时前
五一劳动节快乐 [特殊字符][特殊字符][特殊字符]
java·c++·git·python·学习·github·php