linux/windows 双平台csv文件生成方法

2021-03-17 11:23

阅读:456

标签:value   char   sprintf   fopen   result   file   values   system   移植   

逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。

1、linux/windows 可移植

#include int main()
{
    FILE *fp;
    char const *fileTitle = "num,test content,result\n";
    char const *log_body = ",test_";
    char const *seprate = ",";
    char const *log_tail = "\n";
    char log[64] = {0};

    fp = fopen("./csvtest.csv","wb");
    fprintf(fp,fileTitle);

    for(int i = 0; i 10; i++)
    {
        if(i%2 == 0)
            sprintf(log,"%d%s%d%s%d%s",i,log_body,i,seprate,0,log_tail);
        else
            sprintf(log,"%d%s%d%s%d%s",i,log_body,i,seprate,1,log_tail);
        fprintf(fp,log);
    }


    fclose(fp);
}

2、linux 下也可以直接使用system()

#include 
#include int main()
{
    char log[64] = {0};
    char const *log_head = "echo ";
    char const *log_body = ",test_";
    char const *seprate = ",";
    char const *log_tail = " >> ./csvtest1.csv";
    system("echo num,test content,result >> ./csvtest1.csv");
    for(int i = 0; i 10; i++)
    {
        if(i%2 == 0)
            sprintf(log,"%s%d%s%d%s%d%s",log_head,i,log_body,i,seprate,0,log_tail);
        else
            sprintf(log,"%s%d%s%d%s%d%s",log_head,i,log_body,i,seprate,1,log_tail);
        system(log);
    }
}

 

linux/windows 双平台csv文件生成方法

标签:value   char   sprintf   fopen   result   file   values   system   移植   

原文地址:https://www.cnblogs.com/Malphite/p/12394100.html


评论


亲,登录后才可以留言!