Reading a .csv file through istream_iterator

I have a .csv file that looks like this:

Title 1, Title 2, Title 3, Title 4,
orange, abc, 12.3, 45.9,
tree apple, rto, 34.6, 0.9,
...

My code replaces all commas by spaces and iterates in order to read the file. However,this means that first columns with double names such as "banana forest" will be identified as two separate columns. Is there any way to solve this problem by using iteration?

1
2
3
4
5
6
7
 vector<vector<string> > data;
	ifstream matrix("somefile.csv");
	for (string line; getline(matrix, line); ) {
		replace(line.begin(), line.end(), ',', ',');
		istringstream in(line); 
		data.push_back(vector<string>(istream_iterator<string>(in), istream_iterator<string>()));
	}
Yes, don't replace the commas with spaces. Use the commas as delimiters.

Why must you use iterators? Why not use getline() with the optional third parameter (the delimiter).
Topic archived. No new replies allowed.