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);
	
}
}
相关推荐
柒儿吖几秒前
三方库 Boost.Regex 在 OpenHarmony 的 lycium完整实践
c++·c#·openharmony
一只小小的芙厨7 分钟前
寒假集训·子集枚举2
c++·笔记·算法·动态规划
kyle~12 分钟前
ROS2----组件(Components)
开发语言·c++·机器人·ros2
阿猿收手吧!18 分钟前
【C++】Ranges 工厂视图与投影机制
开发语言·c++
.小墨迹18 分钟前
局部规划中的TEB,DWA,EGOplanner等算法在自动驾驶中应用?
开发语言·c++·人工智能·学习·算法·机器学习·自动驾驶
你的冰西瓜29 分钟前
C++中的priority_queue容器详解
开发语言·c++·stl
柒儿吖36 分钟前
三方库 Emoji Segmenter 在 OpenHarmony 的 lycium 适配与测试
c++·c#·openharmony
冬风诉39 分钟前
cuda核函数
c++·cuda
㓗冽1 小时前
矩阵问题(二维数组)-基础题70th + 发牌(二维数组)-基础题71th + 数字金字塔(二维数组)-基础题72th
c++·算法·矩阵
系统修复专家1 小时前
UG12.0官方未公开修复方法:彻底解决C++异常崩溃问题
开发语言·c++·安全·bug·dll·游戏报错