当存在空单元格时,用于 c++ 的 Tsv 文件解析器会突然结束
Tsv file parser for c++ ends abruptly when empty cell is present
我正在尝试解析 .tsv 文件并将行的每个单元格的值存储在结构中。每一行构成结构并追加到列表中。如果单元格为空,则getline
while 循环突然结束
.tsv 文件如下所示:
No Name Age Grade
1 Andy 17 A
2 Drew 16 B
3 Brad 17 B
4 Cam A
5 Sam 18 B
示例代码
std::ifstream tsvFile(filePath);
if (!tsvFile.good()) return;
for (std::string line; std::getline(tsvFile, line); )
{
example item;
tsvFile >> example.s_no >> example.name >> example.age >> example.grade;
tsv_list.push_back(item);
}
tsvFile.close();
遍历所有行,不会突然停止。有没有更好的方法来逐行解析 tsv 并添加特定的制表符分隔符?我尝试使用line
但值似乎不正确。每次循环访问时,打印line
都会给我一个整数,而不是整行。
这是从文件中读取记录的典型示例。
我们将选择一种面向对象的方法,并将所有数据放入一个结构中,并且,由于结构应该知道如何读取和写入其数据,因此添加提取器和插入器操作器。
提取器将读取一整行,然后将数据放入结构成员中。如果出现错误,可以使用默认值。
主要情况下,我们只需定义 Roster 的向量并使用范围构造函数,在变量定义期间读取完整的输入文件。
之后,我们打印结果。
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <iterator>
// Test Data. Same as reading from file
std::istringstream testFile(R"#(1 Andy 17 A
2 Drew 16 B
3 Brad 17 B
4 Cam A
5 Sam 18 B
)#");
struct Roster
{
// Roster Data
size_t No{}; std::string Name{}; size_t Age{}; char Grade{};
// Extractor operator >> for Roster
friend std::istream& operator >> (std::istream& is, Roster& r) {
std::string line{}; // Here we will store the read line
std::getline(is, line); // Read complete line
std::istringstream iss(line); // Copy to a istringstream for extraction
if (!(iss >> r.No >> r.Name >> r.Age >> r.Grade)) { // Extract
// In case of error: Reset all values
r.No = 0; r.Name = "ERROR"; r.Age = 0; r.Grade = '#';
};
return is;
}
// Inserter operator << . Print space delimited data
friend std::ostream& operator << (std::ostream& os, const Roster& r) {
return os << r.No << ' ' << r.Name << ' ' << r.Age << ' ' << r.Grade;
}
};
int main()
{
// Read complete CSV
std::vector<Roster> roster{ std::istream_iterator<Roster>(testFile), std::istream_iterator<Roster>() };
// Copy all data to output
std::copy(roster.begin(), roster.end(), std::ostream_iterator<Roster>(std::cout, "n"));
return 0;
}
相关文章:
- 通过终端在文件中输入时检测EOF(文件结束)时出现问题
- 带有文件结束函数的 while 循环重复输出文件中的最后一个数字两次
- 触发文件结束的原因
- 从 ifstream 读取,直到文件结束
- C++:读取.BMP文件时出现问题;文件结束时间早于预期
- C 功能:读取直到文件结束 - 查找代码中的错误
- C :在文件结束时GetLine冻结
- 表示文件结束
- 如何让程序知道当我使用 while(cin>> 时文件结束
- 文件结束输入不起作用
- 使用RAII写入文件结束标记
- 在文件结束后继续输入
- .hpp 文件上的意外文件结束错误
- 读取:ASIO中的文件结束.插座为什么要关上
- 防止包括从hxx(模板定义)文件结束头
- fstream对象的文件结束
- 文件不会重复自己,直到文件结束
- 文件结束检查不起作用 (C++)
- 语法错误,意外的标记,期望文件结束
- 如何模拟文件结束