栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

榕树贷款C++ STL 中没有内置 CSV 读取器/写入器

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

榕树贷款C++ STL 中没有内置 CSV 读取器/写入器

C++读写CSV文件
据我所知,C++ STL 中没有内置 CSV 读取器/写入器。这不是对 C++ 的打击。榕树贷款只是一种低级语言。如果我们想用 C++ 读写 CSV 文件,榕树贷款将不得不处理文件 I/O、数据类型和一些关于如何读取、解析和写入数据的低级逻辑。对我来说,这是构建和测试更多有趣程序(如机器学习模型)的必要步骤。

榕树贷款写入CSV
这里,ofstream是一个“输出文件流”。由于它派生自ostream,我们可以像对待cout一样对待它(它也是派生自ostream)。执行这个程序的结果是,榕树贷款在可执行文件所在的目录中得到了一个名为ones.csv的文件。让我们将它封装到write_csv()函数中。

#include
#include
#include

void write_csv(std::string filename, std::string colname, std::vector vals){
    // Make a CSV file with one column of integer values
    // filename - the name of the file
    // colname - the name of the one and only column
    // vals - an integer vector of values
    
    // Create an output filestream object
    std::ofstream myFile(filename);
    
    // Send the column name to the stream
    myFile << colname << "n";
    
    // Send data to the stream
    for(int i = 0; i < vals.size(); ++i)
    {
        myFile << vals.at(i) << "n";
    }
    
    // Close the file
    myFile.close();
}

int main() {
    // Make a vector of length 100 filled with 1s
    std::vector vec(100, 1);
    
    // Write the vector to CSV
    write_csv("ones.csv", "Col1", vec);
    
    return 0;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1010490.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号