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


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

    1、linux/windows 可移植

    #include <stdio.h>
    
    int main()
    {
        FILE *fp;
        char const *fileTitle = "num,test content,result
    ";
        char const *log_body = ",test_";
        char const *seprate = ",";
        char const *log_tail = "
    ";
        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 <stdio.h>
    #include <stdlib.h>
    
    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);
        }
    }
  • 相关阅读:
    实用分页小方法
    Android:Handler实现异步处理功能
    Android--sharepreference总结
    安卓 如何向数据库传值
    Activity 之间的传值
    Android 双击返回键退出程序 实现
    HttpUtitlity.UrlEncode
    android 如何设置背景的透明度
    比较Date时间先后
    iOS 判断字符串中含有某个字符串 rangeOfString
  • 原文地址:https://www.cnblogs.com/Malphite/p/12394100.html
Copyright © 2020-2023  润新知