C++ 读写 CSV 文件
1. CSV格式定义逗号分隔值Comma-Separated ValuesCSV有时也称为字符分隔值因为分隔字符也可以不是逗号其文件以纯文本形式存储表格数据数字和文本。纯文本意味着该文件是一个字符序列不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成记录间以某种换行符分隔每条记录由字段组成字段间的分隔符是其它字符或字符串最常见的是逗号或制表符。通常所有记录都有完全相同的字段序列。通常都是纯文本文件。建议使用WORDPAD或是记事本来开启再则先另存新档后用EXCEL开启也是方法之一。2. 使用标准库读写2.1. 读CSV文件std::vectorstd::pairdouble, 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::vectorstd::pairdouble, Eigen::Isometry3d trajectory; std::string line_str; while (getline(in_file, line_str)) { if (line_str.empty()) { continue; } std::vectorstd::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::vectorstd::vectorstd::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-parser2.2. 读CSV文件void readCSV(const std::string filename, std::vectorstd::vectorstd::string data) { io::CSVReaderMAX_COLS csv(filename); std::vectorstd::string row; while (csv.read_row(row)) { data.push_back(row); } }2.3. 写CSV文件void writeCSV(const std::string filename, const std::vectorstd::vectorstd::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::vectorstd::vectorstd::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::vectorstd::string row; boost::tokenizerboost::escaped_list_separatorchar 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::vectorstd::vectorstd::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中文网