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);
	
}
}
相关推荐
感哥4 小时前
C++ 面向对象
c++
沐怡旸5 小时前
【底层机制】std::shared_ptr解决的痛点?是什么?如何实现?如何正确用?
c++·面试
感哥12 小时前
C++ STL 常用算法
c++
saltymilk1 天前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥1 天前
C++ lambda 匿名函数
c++
沐怡旸1 天前
【底层机制】std::unique_ptr 解决的痛点?是什么?如何实现?怎么正确使用?
c++·面试
感哥1 天前
C++ 内存管理
c++
博笙困了1 天前
AcWing学习——双指针算法
c++·算法
感哥1 天前
C++ 指针和引用
c++
感哥2 天前
C++ 多态
c++