通过c++解析csv
parsing csv via C++
晚上好,我有以下问题。我正在解析csv文件,像这样:
entry1;entry2;entry3
entry4;entry5;entry6
;;
我是这样获取条目的:
stringstream iss;
while(getline(file, string) {
iss << line;
while(getline(iss, entry, ';') {
/do something
}
}
但是我在最后一行(;;)中遇到了一个问题,我只读取了2个条目,我需要读取第三个空白条目。我该怎么做呢?
首先,我应该指出代码中的一个问题,您的iss
在读取第一行然后调用while(getline(iss, entry, ';'))
后处于失败状态,因此在读取每行后您需要重置stringstream
。处于失败状态的原因是调用std:getline(iss, entry, ';'))
后到达流上的文件结束。
对于您的问题,一个简单的选择是简单地检查是否有任何内容被读入entry
,例如:
stringstream iss;
while(getline(file, line)) {
iss << line; // This line will fail if iss is in fail state
entry = ""; // Clear contents of entry
while(getline(iss, entry, ';')) {
// Do something
}
if(entry == "") // If this is true, nothing was read into entry
{
// Nothing was read into entry so do something
// This doesn't handle other cases though, so you need to think
// about the logic for that
}
iss.clear(); // <-- Need to reset stream after each line
}
相关文章:
- 使用 istream 提取运算符进行 csv 解析:如何检测缺失的字段值?
- 获取线函数不会解析 CSV 文件中的行尾
- 解析具有提升精神的简单 csv 表
- Boost Tokenizer无法解析具有带有双引号的字段的CSV文件
- 如何使用 Boost 内存映射解析 C++ 中的 CSV?
- 在 C++ 中解析 csv 退出代码 11
- 在开关盒循环中使用未删除的标识符来解析 CSV 文件
- 如何使用没有清晰格式样式的C 解析CSV文件
- 解析 csv 文件,"malloc: *** error for object 0x7ffeeb4f4b80: pointer being freed was not allocated"出现此错误
- 解析一般CSV读取功能时,如何处理不同的数据类型?(不明确指定它们)
- 解析从 Excel 导出Microsoft csv 文件
- 具有多个 EOL 字符的常规 CSV 解析器
- 我应该如何在C++中读取和解析(真实的、正确的、功能齐全的)CSV
- 提升精神解析 CSV,列顺序可变
- 按行解析和排序 csv 文件
- 解析 csv 文件 c++
- C++中的快速CSV解析器
- 引号中带有逗号的C++CSV解析
- CSV解析器的性能瓶颈
- C++ - 无法将 CSV 解析为我的结构