编辑框保存为csv文件

1.新建基于MFC的对话框,并为对话框添加控件及绑定变量

https://blog.csdn.net/zhang_fei_fresh/article/details/76408636

http:https://blog.csdn.net/qq_33723441/article/details/102954431 MFC读写CSV文件

2.为写入文件按钮添加事件函数

void C编辑框保存为csv文件Dlg::OnBnClickedButton1()
{
	// TODO:  在此添加控件通知处理程序代码
		CStdioFile file;
		file.Open(_T("D:\\aaa.csv"), CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite,NULL);

		file.SeekToEnd();

		CString str;

		GetDlgItemText(IDC_EDIT1, str);
		double d1 = _wtof((LPCTSTR)str);//https://blog.csdn.net/weixin_43619346/article/details/107297091

		GetDlgItemText(IDC_EDIT2, str);
		double d2 = _wtof((LPCTSTR)str);

		GetDlgItemText(IDC_EDIT3, str);
		double d3 = _wtof((LPCTSTR)str);

		GetDlgItemText(IDC_EDIT4, str);
		double d4 = _wtof((LPCTSTR)str);
		COleDateTime t = COleDateTime::GetCurrentTime();//https://learn.microsoft.com/zh-cn/cpp/atl-mfc-shared/reference/coledatetime-class?view=msvc-170#getcurrenttime


		CString str_time;
		str_time.Format(_T("%d.%d.%d "), t.GetYear(), t.GetMonth(), t.GetDay());
		SetDlgItemText(IDC_EDIT5, str_time);

		CString str_time2;
		str_time.Format(_T("%d:%d:%d "), t.GetHour(), t.GetMinute(), t.GetSecond());
		SetDlgItemText(IDC_EDIT6, str_time);
		UpdateData(FALSE);

		str.Format(
			_T("%d.%d.%d ")
			_T(", %d:%d:%d ")
			_T(", %f , %f , %f , %f")
			_T("\n")
			, t.GetYear(), t.GetMonth(), t.GetDay()
			, t.GetHour(), t.GetMinute(), t.GetSecond()
			, d1, d2, d3, d4);

		file.WriteString(str);

		file.Close();

}

3.为打开文件按钮添加事件函数

void C编辑框保存为csv文件Dlg::OnBnClickedButton2()
{
	// TODO:  在此添加控件通知处理程序代码
	//读csv文件///

		CStdioFile file;
		file.Open(_T("D:\\aaa.csv"), CFile::modeRead);

		CString str;
		while (file.ReadString(str))
		{
			str = str.Trim(_T(" "));

			CString substr[10];
			int count = 0;

			int index = str.Find(_T(","));
			while (index != -1 && count<9)
			{
				substr[count++] = str.Left(index);
				str = str.Right(str.GetLength() - index - 1);
				index = str.Find(_T(","));
			}
			substr[count++] = str;

			CString stmp;
			stmp.Format(_T("%s %s %s %s %s %s %s %s %s %s")
				, substr[0], substr[1], substr[2], substr[3], substr[4]
				, substr[5], substr[6], substr[7], substr[8], substr[9]
				);
			MessageBox(stmp);
		}

		file.Close();
}

4.运行结果

  • 13
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值