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.运行结果