c++中csv文件读写的方法是什么


在C++中,要读写CSV文件可以使用标准库中的fstream库。下面是一个简单的示例代码:

#include <iostream>#include <fstream>#include <sstream>#include <vector>int main() {std::ofstream outfile("data.csv");if (!outfile.is_open()) {std::cerr << "Error opening file." << std::endl;return 1;}outfile << "Name,Age,Score" << std::endl;outfile << "Alice,25,90" << std::endl;outfile << "Bob,30,85" << std::endl;outfile.close();std::ifstream infile("data.csv");if (!infile.is_open()) {std::cerr << "Error opening file." << std::endl;return 1;}std::string line;std::vector<std::vector<std::string>> data;while (std::getline(infile, line)) {std::stringstream ss(line);std::vector<std::string> row;std::string cell;while (std::getline(ss, cell, ',')) {row.push_back(cell);}data.push_back(row);}infile.close();for (const auto& row : data) {for (const auto& cell : row) {std::cout << cell << " ";}std::cout << std::endl;}return 0;}

在上面的代码中,首先我们创建了一个名为"data.csv"的CSV文件,并向文件中写入了一些数据。然后我们再次打开文件并读取数据,将数据存储在一个二维vector中,最后打印出数据内容。


上一篇:c++怎么读取csv文件中的数据

下一篇:ConcurrentHashMap支持哪些原子操作


c++
Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器