news 2026/5/7 11:56:24

C++ 读写 CSV 文件

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++ 读写 CSV 文件

1. CSV格式定义

逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。通常,所有记录都有完全相同的字段序列。通常都是纯文本文件。建议使用WORDPAD或是记事本来开启,再则先另存新档后用EXCEL开启,也是方法之一。

2. 使用标准库读写

2.1. 读CSV文件

std::vector<std::pair<double, Eigen::Isometry3d>> LoadTrajectoryFromFile(const std::string &pose_file) { std::ifstream in_file(pose_file); if (!in_file.is_open()) { LOG(FATAL) << "Unable to open file: " << pose_file; } std::vector<std::pair<double, Eigen::Isometry3d>> trajectory; std::string line_str; while (getline(in_file, line_str)) { if (line_str.empty()) { continue; } std::vector<std::string> strs = StringSplit(line_str, ","); if (strs.size() != 8) { LOG(INFO) << "Find invalid pose line, size: " << strs.size() << line_str; continue; } double timestamp = std::stod(strs[0]); Eigen::Vector3d translation = {std::stod(strs[1]), std::stod(strs[2]), std::stod(strs[3])}; Eigen::Quaterniond quater = {std::stod(strs[7]), std::stod(strs[4]), std::stod(strs[5]), std::stod(strs[6])}; quater.normalized(); Eigen::Isometry3d pose = Eigen::Isometry3d::Identity(); pose.translation() = translation; pose.linear() = quater.toRotationMatrix(); trajectory.emplace_back(std::make_pair(timestamp, pose)); } in_file.close(); return trajectory; }

2.2. 写CSV文件

void writeCSV(const std::string& filename, const std::vector<std::vector<std::string>>& data) { std::ofstream file(filename); if (!file.is_open()) { std::cout << "Failed to open the file." << std::endl; return; } for (const auto& row : data) { for (size_t i = 0; i < row.size(); ++i) { file << row[i]; if (i < row.size() - 1) { file << ","; } } file << std::endl; } file.close(); }

2. 使用csv-parser读写

2.1. 安装

sudo npm install -g csv-parser

2.2. 读CSV文件

void readCSV(const std::string& filename, std::vector<std::vector<std::string>>& data) { io::CSVReader<MAX_COLS> csv(filename); std::vector<std::string> row; while (csv.read_row(row)) { data.push_back(row); } }

2.3. 写CSV文件

void writeCSV(const std::string& filename, const std::vector<std::vector<std::string>>& data) { std::ofstream file(filename); if (!file.is_open()) { std::cout << "Failed to open the file." << std::endl; return; } for (const auto& row : data) { for (size_t i = 0; i < row.size(); ++i) { file << row[i]; if (i < row.size() - 1) { file << ","; } } file << std::endl; } file.close(); }

3. 使用boost库读写

3.1. 读CSV文件

#include <boost/algorithm/string.hpp> #include <boost/tokenizer.hpp> void readCSV(const std::string& filename, std::vector<std::vector<std::string>>& data) { std::ifstream file(filename); if (!file.is_open()) { std::cout << "Failed to open the file." << std::endl; return; } std::string line; while (std::getline(file, line)) { std::vector<std::string> row; boost::tokenizer<boost::escaped_list_separator<char>> tokenizer(line); for (const auto& cell : tokenizer) { row.push_back(cell); } data.push_back(row); } file.close(); }

3.2. 写CSV文件

#include <boost/algorithm/string.hpp> #include <boost/tokenizer.hpp> void writeCSV(const std::string& filename, const std::vector<std::vector<std::string>>& data) { std::ofstream file(filename); if (!file.is_open()) { std::cout << "Failed to open the file." << std::endl; return; } for (const auto& row : data) { std::string line; for (size_t i = 0; i < row.size(); ++i) { line += row[i] + ","; } line = line.substr(0, line.size() - 1); file << line << std::endl; } file.close(); }

参考文献

csv是什么格式文件-常见问题-PHP中文网

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/7 11:56:21

Arm CoreSight ELA-600调试跟踪系统常见错误与解决方案

1. Arm CoreSight ELA-600调试跟踪系统概述在嵌入式系统开发领域&#xff0c;调试跟踪技术如同医生的听诊器&#xff0c;是诊断复杂系统问题的关键工具。Arm CoreSight架构作为业界广泛采用的调试解决方案&#xff0c;其ELA-600&#xff08;Embedded Logic Analyzer&#xff09…

作者头像 李华
网站建设 2026/5/7 11:55:03

PCL 库特征提取

PCL (Point Cloud Library) 是用于处理2D/3D 图像以及点云的一个大型开源项目。学习PCL最好的途径是阅读其官网文档&#xff08;Point Cloud Library (PCL)&#xff09;。虽然PCL的网站文档稍微有点“丑”&#xff0c;但是其内容十分详尽。从应用的角度而言&#xff0c;PCL可以…

作者头像 李华
网站建设 2026/5/7 11:55:00

哈密顿回路、链路、其他点覆盖问题

目录 一&#xff0c;哈密顿回路、哈密顿链路 二&#xff0c;相关定理 1&#xff0c;Dirac定理 2&#xff0c;Tutte定理 3&#xff0c;竞赛图必有哈密顿链路 三&#xff0c;应用 1&#xff0c;旅行商问题&#xff08;TSP&#xff09; 2&#xff0c;芯片通孔 四&#xf…

作者头像 李华