答案:使用C++标准库解析CSV需处理引号内逗号与转义,通过逐字符判断引号状态实现准确分割。

在不引入第三方库的情况下,用C++解析CSV文件可以通过标准库中的fstream和stringstream来实现。虽然CSV看似简单,但实际中可能包含逗号在引号内、换行符、空字段等复杂情况。下面介绍一种实用且健壮的解析方法。
通过std::ifstream打开并逐行读取CSV文件,再使用std::getline配合std::stringstream拆分字段。
基本步骤:
标准CSV允许字段用双引号包围,其中可包含逗号或换行。例如:
Name,Age,Location
"Smith, John",35,"New York, NY"
要正确解析这类内容,不能简单地按逗号切分。需要手动解析状态:是否在引号内。
立即学习“C++免费学习笔记(深入)”;
简化策略(适用于大多数场景):
以下是一个能处理带引号字段的基础实现:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
std::vector<std::vector<std::string>> parseCSV(const std::string& filename) {
std::ifstream file(filename);
std::vector<std::vector<std::string>> data;
std::string line;
while (std::getline(file, line)) {
std::vector<std::string> row;
std::string field;
bool inQuote = false;
for (size_t i = 0; i < line.size(); ++i) {
char c = line[i];
if (c == '\"') {
if (i + 1 < line.size() && line[i+1] == '\"') {
// 转义的双引号 ""
field += '\"';
++i;
} else {
inQuote = !inQuote;
}
} else if (c == ',' && !inQuote) {
row.push_back(field);
field.clear();
} else {
field += c;
}
}
row.push_back(field); // 添加最后一个字段
data.push_back(row);
}
return data;
}
调用方式:
auto csvData = parseCSV("data.csv");
for (const auto& row : csvData) {
for (const auto& field : row) {
std::cout << "[" << field << "] ";
}
std::cout << "\n";
}
该方法适用于大多数常规CSV文件,但仍有几点需要注意:
基本上就这些。用标准C++就能写出稳定可用的CSV解析器,关键是正确处理引号和转义。不复杂但容易忽略细节。
以上就是c++++怎么在不引入第三方库的情况下解析CSV文件_C++文件处理与CSV解析技巧的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号