C++ reading from a multi-type csv file
I'm trying to use C++ to read from a file, which is structured like this:
frank, 80, 90
johnny, 10, 25
...
I made a loop to go through each line of the file, but every time I read the name string, the comma is included with the name (so instead of frank
I get frank,
).
My code in question is:
// var declarations
ifstream streamVar;
string name;
int num1, num2;
// there is a chunk of code that opens file and does error chec开发者_Go百科king here
// this is the code that I'm having trouble with
streamVar >> name;
streamVar >> num1;
streamVar.ignore(100, ',');
streamVar >> num2;
How can I read these three values while ignoring the commas?
IIRC the streams use space as a default delimiter and you will have to specify that ',' is another delimiter.
Here's another question on SO regarding specifying delimiters, that should solve your problem.
Must be because your code is wrong. Exactly how is anyone's guess since you failed to post it.
I've posted the code I use to read and write CSV files in C++. Perhaps that will be some help.
精彩评论