BOOL WriteCsv(CString strText) { CString strPath; strPath.Format(_T("D:\\CSV")); if (!CFileSystemHelper::IsDirectoryExist(strPath))//判断目录存不存在 { CFileSystemHelper::CreateDirectory(strPath); //可创建递归目录 } CString strFileNameTimeStamp= _T("%4d%2d%2d");//年月日 SYSTEMTIME time; ::GetLocalTime(&time);
CString strFileNameDateTime, strLogFilePath; strFileNameDateTime.Format(strFileNameTimeStamp, time.wYear, time.wMonth, time.wDay); strFileNameDateTime.Replace(' ', '0'); strLogFilePath.Format(_T("%s\\%s.csv"), strPath, strFileNameDateTime);//文件名可定义,这里以时间作为文件名 CString strData=_T(""); CString strTitle = _T(""); CFileFind find; if (!find.FindFile(strLogFilePath))//文件存在就不用再创建表头 { strTitle+=_T("日期时间,"); strTitle+=_T("Data,"); strTitle+=_T("\n"); } CTime tm = CTime::GetCurrentTime(); CString strTime = _T(""); strTime = tm.Format(_T("%Y-%m-%d %H:%M:%S")); strData += strTime; strData+=_T(",");//以逗号分隔数据 strData+=strText; strData+=_T(","); strData+=_T("\n"); CStdioFile file; char *oldLocale = NULL; if (file.Open(strLogFilePath, CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite)) { oldLocale = _strdup(setlocale(LC_CTYPE,NULL)); setlocale(LC_CTYPE,"chs"); file.SeekToEnd(); if (strTitle.GetLength() > 0) { file.WriteString(strTitle); } file.WriteString(strData); setlocale(LC_CTYPE,oldLocale); free(oldLocale); file.Close(); } else { return FALSE; } return TRUE; }
标签:MFC,strData,CString,strTitle,file,time,格式,CSV,strPath From: https://www.cnblogs.com/St3ven/p/17890615.html