C++ 导出CSV文件


#include <string>
#include <fstream>
#include <sstream>

class CsvStream
{
    std::ofstream fs_;
    bool is_first_;
    const std::string separator_;
    const std::string escape_seq_;
    const std::string special_chars_;

public:
    CsvStream(const std::string filename, const std::string separator = ",")
        : fs_()
        , is_first_(true)
        , separator_(separator)
        , escape_seq_("\"")
        , special_chars_("\"")
    {
        fs_.exceptions(std::ios::failbit | std::ios::badbit);
        fs_.open(filename.c_str());
    }

    ~CsvStream()
    {
        flush();
        fs_.close();
    }

    void flush()
    {
        fs_.flush();
    }

    inline static CsvStream& endl(CsvStream& file)
    {
        file.endrow();
        return file;
    }

    void endrow()
    {
        fs_ << std::endl;
        is_first_ = true;
    }

    CsvStream& operator<<(CsvStream& (*val)(CsvStream&))
    {
        return val(*this);
    }

    CsvStream& operator<<(const char* val)
    {
        return write(escape(val));
    }

    CsvStream& operator<<(const std::string& val)
    {
        return write(escape(val));
    }

    template<typename T>
    CsvStream& operator<<(const T& val)
    {
        return write(val);
    }

private:
    template<typename T>
    CsvStream& write(const T& val)
    {
        if (!is_first_)
        {
            fs_ << separator_;
        }
        else
        {
            is_first_ = false;
        }
        fs_ << val;
        return *this;
    }

    std::string escape(const std::string& val)
    {
        std::ostringstream result;
        result << '"';
        std::string::size_type to, from = 0u, len = val.length();
        while (from < len && std::string::npos != (to = val.find_first_of(special_chars_, from)))
        {
            result << val.substr(from, to - from) << escape_seq_ << val[to];
            from = to + 1;
        }
        result << val.substr(from) << '"';
        return result.str();
    }
};

void test() {
    CsvStream file("test.csv");
    file << "col1" << "col2" << "col3" << CsvStream::endl;
    file << "val1" << "val2" << "val3" << CsvStream::endl;
    file << "val1" << "val2" << "val3" << CsvStream::endl;
    file.flush();
}

输出 


创作不易,小小的支持一下吧!

相关推荐

  1. c#中将数据库中的文件导出csv、xml文件的demo

    2024-07-16 00:52:03       21 阅读
  2. SpringBoot整合easyExcel实现CSV格式文件导入导出

    2024-07-16 00:52:03       63 阅读
  3. c#读取CSV文件跟Excel导入成DataTble

    2024-07-16 00:52:03       48 阅读
  4. C#win form解决导入CSV文件数据缺失问题

    2024-07-16 00:52:03       44 阅读
  5. C# Cad 文字信息导入导出(八)

    2024-07-16 00:52:03       60 阅读
  6. C++写csv文件

    2024-07-16 00:52:03       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 00:52:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 00:52:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 00:52:03       58 阅读
  4. Python语言-面向对象

    2024-07-16 00:52:03       69 阅读

热门阅读

  1. 微信小程序学习使用问题总结

    2024-07-16 00:52:03       20 阅读
  2. 数据集配置

    2024-07-16 00:52:03       21 阅读
  3. windows区分大小写

    2024-07-16 00:52:03       23 阅读
  4. BCC工具命令报错解决步骤

    2024-07-16 00:52:03       21 阅读
  5. React

    React

    2024-07-16 00:52:03      19 阅读
  6. zookeeper+kafka消息队列群集部署

    2024-07-16 00:52:03       18 阅读