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);
	
}
}
相关推荐
888CC++5 小时前
C语言与C++的区别:从面向过程到面向对象
java·c语言·c++
Darkwanderor6 小时前
Linux系统编程实战项目:模拟实现shell
linux·c++
himobrinehacken7 小时前
揭秘Windows程序启动的神秘之旅
c++·安全
库玛西7 小时前
C++ 运行时多态 :核心总结与原理图解
c语言·c++·笔记
Byron Loong8 小时前
【C++】重定向是什么
开发语言·c++
hansang_IR9 小时前
【题解】LC:Z 算法(Z Algorithm)
c++·算法·字符串
霍霍的袁11 小时前
【C++】模板从入门到进阶(函数模板 + 类模板 + 特化 + 分离编译)
开发语言·c++·visual studio
海清河晏11111 小时前
Qt 实战:信号与槽+事件系统
开发语言·c++·qt
hansang_IR11 小时前
【题解】LC:卷积(Convolution (Mod 1,000,000,007))
c++·算法·ntt·crt
计算机内卷的N天11 小时前
三维C三维配准软件(术中CT)
c++·c臂三维配准