lundi 16 mars 2015

Having trouble parsing simple text file separated by commas


I am having trouble parsing my text file which is separated my commas. istream and ifstream really confuse me.


Here is my text file:



2,2,Robin Williams
3,2,Bud Abbott,Lou Costello
4,2
5,4,Groucho,Chico,Harpo,Gummo
6,3
7,2,W.C. Fields
9,5


and here is my code



#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream inf("test.txt");
while(!inf.eof())
{
string line;
int roomNumber;
inf >> roomNumber;
getline(inf, line);
cout << roomNumber << endl;
}
}


My end goal is to separate each field by commas and store them somewhere. The first two numbers will always be therre while the names can be empty. Right now I am just trying to store the first integer, the roomNumber, and printing them out but my output looks like this:



2
3
4
5
6
7
9
9


Why are there two nines and is there any better way to do this?




Aucun commentaire:

Enregistrer un commentaire