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);
	
}
}
相关推荐
哇哈哈20212 小时前
信号量和信号
linux·c++
多恩Stone2 小时前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马3 小时前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝3 小时前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc4 小时前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼4 小时前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛
yyjtx4 小时前
DHU上机打卡D31
开发语言·c++·算法
czxyvX5 小时前
020-C++之unordered容器
数据结构·c++
会编程的土豆5 小时前
2.25 做题
数据结构·c++·算法
Ljwuhe6 小时前
类与对象(中)——运算符重载
开发语言·c++