首页 > 其他分享 >[MFC]如何将数据保存为CSV格式

[MFC]如何将数据保存为CSV格式

时间:2023-12-09 10:44:06浏览次数:27  
标签:MFC strData CString strTitle file time 格式 CSV strPath

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

相关文章

  • vue 格式化JSON
    第一步:安装npmibin-code-editor-S#oryarnaddbin-code-editor第二步:在main.js中引入//引入vueimportVuefrom'vue';//引入bin-code-editor相关插件和样式importCodeEditorfrom'bin-code-editor';import'bin-code-editor/lib/styles/index.css�......
  • Python 输入输出与文件处理: io、pickle、json、csv、os.path 模块详解
    Python提供了强大的输入输出和文件处理工具,通过io、pickle和json等模块,开发者可以轻松处理文件、序列化和反序列化数据,并在不同格式之间进行转换。在本文中,我们将深入介绍这些模块的用法和实际示例。1.io模块:强大的输入输出工具io模块提供了对文件I/O进行灵活处理的能力......
  • Python 输入输出与文件处理: io、pickle、json、csv、os.path 模块详解
    Python提供了强大的输入输出和文件处理工具,通过io、pickle和json等模块,开发者可以轻松处理文件、序列化和反序列化数据,并在不同格式之间进行转换。在本文中,我们将深入介绍这些模块的用法和实际示例。1.io模块:强大的输入输出工具io模块提供了对文件I/O进行灵活处理的能力......
  • Python利用pandas拆分大型的csv文件
    当将大型CSV文件拆分成较小的文件时,您可以使用Python的Pandas库来处理导入必要的库importpandasaspd定义拆分函数当定义拆分函数时,我们将在以下步骤中处理CSV文件的拆分defsplit_csv(input_file,chunk_size):#读取CSV文件dtype_options={'column9':str......
  • scrapy框架之格式化&持久化
    格式化处理在parse方法中直接处理是简单的处理方式,不太建议,如果对于想要获取更多的数据处理,则可以利用Scrapy的items将数据格式化,然后统一交由pipelines来处理以爬取校花网校花图片相关信息为例:1importscrapy2fromscrapy.selectorimportHtmlXPathSelector3froms......
  • bin、hex、axf和elf文件格式
    参考文章:https://www.eet-china.com/mp/a37431.html 在嵌入式软件开发中,bin、hex、axf和elf这四种格式的文件很常见。 bin文件bin是binary的缩写,直白的翻译即为二进制文件。bin是一种最简单的程序文件,只有程序数据。因此我们在下载bin程序文件的时候,必须要设置起始地......
  • Springboot+FastJson实现解析第三方http接口json数据为实体类(时间格式化转换、字段包
    场景若依前后端分离版手把手教你本地搭建环境并运行项目:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662在上面搭建SpringBoot项目的基础上,并且在项目中引入fastjson、hutool、lombok等所需依赖后。系统需要对接第三方http接口获取返回的数据,并将json数......
  • How to get printk format specifiers right (如何正确使用printk格式说明符)(翻译 by
    原文:https://www.kernel.org/doc/html/latest/core-api/printk-formats.html#printk-specifiers如何正确使用printk格式说明符整数类型如果变量是Type类型,则使用printk格式说明符:signedchar%d或%hhxunsignedchar%u或%xchar......
  • 封装url得到响应并修改返回格式为Map
    //封装url并得到返回数据privateResponseEntity<String>getResponse(Stringurl,Map<String,String>request){//它提供了一组方便的方法,可以发送HTTP请求并处理响应RestTemplaterestTemplate=newRestTemplate();//创建请求头并设置Content-Type为applic......
  • pem格式转换ckps12(含证书链)
    转换地址:https://tools.imtrust.cn/#/cert-utils/cert_convert.html1.公钥包含证书链一般命名为 cer.域名.zip把里面两个.cer内容全部用vscode打卡,依次把_.域名_zheng_shu.cer的内容_.域名_zheng_shu_lian.cer的内容放入公钥栏2.私钥包一般命名为 csr.域名.zip把里面的_.......