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 checking 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.