【发布时间】:2016-08-29 06:54:43
【问题描述】:
我比较了 csv 文件中的两个字符串。 由于解析csv文件的结果是得到逗号分隔的数据行。
编码工作正常,但无法显示最后匹配的行。
CSV 文件内容:
Title,sn,sn,sn
test,123,344,222
test,123,344,222
test,123,344,222
test,123,344,222
test,456,677,223
test,5,4545,32
apple,23,44,22
apple,323,23,22
例如,我的代码只显示了缺少最后匹配行的内容,
Title,sn,sn,sn
test,123,344,222
test,123,344,222
test,123,344,222
test,123,344,222
test,456,677,223
而不是,
Title,sn,sn,sn
test,123,344,222
test,123,344,222
test,123,344,222
test,123,344,222
test,456,677,223
test,5,4545,32
代码如下:
int main()
{
string line;
ifstream file("sample.csv");
if(!file)
{
cout << "Error, could not open file." << endl;
return -1;
}
while(getline(file, line))
{
stringstream ss(line);
string line2;
getline(file, line2, ',');
string str = "test";
if(line2 == str)
{
cout << line << endl;
}
}
}
【问题讨论】:
标签: c++ string csv compare getline