简单易用的OpenCSV使用教程

2023-07-20 17:34:14 浏览数 (1)

简单易用的OpenCSV使用教程

跨平台多线程设计!

使用C 分析大数据的时候,数据按CSV格式导出,可以用Excel分析数据。

OpenCSV非常简单易用。

https://github.com/OpenMiniServer

跨平台支持

Windows、linux、Mac、iOS、Android等跨平台设计

编译和执行

请安装cmake工具,用cmake可以构建出VS或者XCode工程,就可以在vs或者xcode上编译运行。

源代码:https://github.com/OpenMiniServer/opencsvhttps://github.com/OpenMiniServer/opencsv)

代码语言:txt复制
git clone https://github.com/OpenMiniServer/opencsv
cd ./opencsv
mkdir build
cd build
cmake ..
#如果是win32,在该目录出现opencsv.sln,点击它就可以启动vs写代码调试
make
./test

全部源文件

  • src/opencsv.h
  • src/opencsv.cpp

1.生成csv

代码语言:C 复制
#include <assert.h>
#include "opencsv.h"
int main()
{
    //make csv
    OpenCSV csv = { "ID", "name", "salary" };
    csv = { "1", "Jack", "100000" };
    csv = { "2", "Tom", "80000" };
    csv = { std::to_string(3), "Lucy", "50000" };

    std::string buffer1;
    csv >> buffer1;
    
    std::string buffer2 = "ID,name,salaryn"
            "1,Jack,100000n"
            "2,Tom,80000n"
            "3,Lucy,50000n";

    assert(buffer1 == buffer2);
    return 0;
}

2.加载csv

代码语言:C 复制
#include <assert.h>
#include "opencsv.h"
int main()
{
    std::string buffer2 = "ID,name,salaryn"
            "1,Jack,100000n"
            "2,Tom,80000n"
            "3,Lucy,50000n";

    OpenCSV csv;
    csv << buffer2;

    std::string buffer1 = "ID,name,salaryn";
    for (size_t i = 1; i < csv.size();   i)
    {
        auto& line = csv[i];
        buffer1.append(line["ID"]   ","   line["name"]   ","   line["salary"]   "n");
    }
    assert(buffer1 == buffer2);
    return 0;
}

3.生成和加载csv文件

代码语言:C 复制
#include <assert.h>
#include "opencsv.h"

int main()
{
    std::string buffer1;
    std::string buffer2;
    buffer2 = "ID,name,salaryn"
            "1,Jack,100000n"
            "2,Tom,80000n"
            "3,Lucy,50000n";

    std::string filePath = "./test.csv";
    //make csv file
    {
        OpenCSV csv;
        csv << buffer2;
        assert(csv[0][0] == "ID");
        assert(csv[0][2] == "salary");
        csv[1]["salary"] = std::to_string(10000);
        csv[2]["salary"] = "8000";
        csv[3]["salary"] = "5000";
        csv >> filePath;
    }
    buffer2 = "ID,name,salaryn"
        "1,Jack,10000n"
        "2,Tom,8000n"
        "3,Lucy,5000n";

    //read csv file
    {
        OpenCSV csv;
        csv << filePath;
        buffer1.clear();
        csv >> buffer1;
    }
    assert(buffer1 == buffer2);

    return 0;
}

0 人点赞