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);
	
}
}
相关推荐
我不是懒洋洋几秒前
从零实现一个分布式监控:Prometheus的核心设计
c++
An_s8 分钟前
c++对接pdfium(一)win系统篇
开发语言·c++
众少成多积小致巨21 分钟前
C++ 规范参考(上)
c++
一拳一个呆瓜4 小时前
【STL】iostream 编程:使用提取运算符
c++·stl
我不是懒洋洋5 小时前
从零实现一个分布式日志平台:ELK的核心设计
c++
神仙别闹5 小时前
基于QT(C++)实现Windows 自启动项查看和分析
c++·windows·qt
WWTYYDS_6666 小时前
ProtoBuf超详细使用教程
c++
野生风长6 小时前
c++类和对象(this指针,重载operator,习题总结)
java·开发语言·c++
雪的季节6 小时前
Python「假多态」与 C++「真多态」的核心区别
开发语言·c++
zmzb01036 小时前
C++课后习题训练记录Day166
开发语言·c++