我正在尝试使用 istream <<(operator) 进行简单的 csv(空格分隔值)解析。
我的文件 csv 文件格式如下所示:
文件.csv
/名字 | 价值 | 间隔 |
姓名1 11 1
姓名2 22 2
姓名3 33 3
我的示例代码如下所示:
fstream fin
std::pair<std::string, struct>entry{};
/* handle empty file path */
if(confPath != nullptr)
{
fin.open(confPath,ios::in);
/* handled no file on the specified path */
if(fin)
{
//check if the file is empty
if(fin.peek() != std::ifstream::traits_type::eof())
{
while(fin >> entry.first)
{
/* Take care of comments, empty lines and spaces */
if(entry.first[0] != '#' && entry.first[0] != '/' && entry.first[0] != '\n')
{
/* Populate the structure with properties from the csv file */
fin >> entry.second.value >> entry.second.interval >> endl;
}
else
{
fin.ignore(256, '\n');
}
}
}
else
{
cout << "file is empty" << endl;
}
}
else
{
cout << "file does not exists" << endl;
}
}
我的代码在使用空行或注释或随机空格时运行良好,但如果缺少其中一个值,它将失败。例如,在 name2 行中,如果缺少值 22,则提取运算符会将 2 解释为该值,并且间隔将设置为 0,并且不会继续解析下一行。
我想知道是否存在一种简单的解决方法来检测 csv 文件中缺少的字段。我可以忽略缺少某些字段但解析继续以下行的那一行。
我查看了一些选项,例如 istream::get、getline、gcount、peek,但我想不出任何简单的解决方案。目前,我无法更改 csv 格式本身。