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);
	
}
}
相关推荐
csdn_aspnet14 小时前
C++ 求给定点集的简单闭合路径(Find Simple Closed Path for a given set of points)
c++
艾莉丝努力练剑14 小时前
【AI接入大模型SDK】解决接口连接异常问题
前端·c++·人工智能·学习·ai·架构
Severus_black14 小时前
【C++初阶】模板进阶
c语言·c++·学习
艾莉丝努力练剑16 小时前
【AI接入大模型SDK】Markdown格式解析
前端·c++·人工智能·学习·ai·架构
逐光沧海17 小时前
数据结构-链表习题(C++)
数据结构·c++·链表
是main不是漫1 天前
【快慢指针 | LeetCode202 快乐数】鸽巢原理佐证环特性,快慢指针巧解快乐数
c语言·开发语言·c++·算法
Sashtok1 天前
cpp基础知识(10)
c++·后端
whitelbwwww1 天前
CUDA C++实战--中级
开发语言·前端·c++
ShineWinsu1 天前
对于Qt:QMessageBox的解析
开发语言·c++·qt·面试·客户端·qmessagebox·消息对话框
小灰灰搞电子2 天前
C++ initializer_list 详解
c++·list