Help reading csv file
Mar 24, 2015 at 8:26pm UTC
Hello, so I'm trying to read in a csv file but running into a lot of
errors bugs.
The file format (obviously) is something like
1.4, 3.4, 6.7,
4.3, , 7.6
The blank bit on the second line is whitespace, but I still want to read it as I'm storing the read in data as strings and extracting data later.
I've tried this line of code
1 2 3 4 5 6
string line;
getline(FILE,line);
istringstream s(line);
string field;
getline(s,field,',' );
cout << field << endl;
but this just gives out the first numbers in the columns & I don't know why.I thought that
getline(s,field,',' );
would read up to the comma and then keep reading until the end of the line
Last edited on Mar 24, 2015 at 8:34pm UTC
Mar 24, 2015 at 9:09pm UTC
I thought that getline(s,field,','); would read up to the comma
Yes
and then keep reading until the end of the line
It leaves the rest in input stream for subsequent reads.
Use getline in a loop to read all values:
1 2 3 4
istringstream s(line);
string field;
while (getline(s,field,',' ))
cout << field << endl;
Mar 24, 2015 at 9:46pm UTC
I'd sorted this out anyway but that's what I did. Cheers =D
Topic archived. No new replies allowed.