我正在尝试使用 C++ 从文件中读取,该文件的结构如下:
frank, 80, 90
johnny, 10, 25
...
我做了一个循环来遍历文件的每一行,但每次我读取名称字符串时,逗号都包含在名称中(所以我得到的不是 frank
frank ,
).
我的问题代码是:
// var declarations
ifstream streamVar;
string name;
int num1, num2;
// there is a chunk of code that opens file and does error checking here
// this is the code that I'm having trouble with
streamVar >> name;
streamVar >> num1;
streamVar.ignore(100, ',');
streamVar >> num2;
如何在忽略逗号的情况下读取这三个值?
最佳答案
IIRC 流使用空格作为默认分隔符,您必须指定“,”是另一个分隔符。
Here's another question on SO regarding specifying delimiters ,那应该可以解决您的问题。
关于C++ 从多类型 csv 文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7019589/