I'm reaidng in values from a CSV value and am storing them in a 2x2 vector that is being dynamically allocated. My CSV file has 12,649 rows, but my vector only 12,484. Also, there seems to be a column missing. I went with a vector here because although I know how many columns to except, I don't know how many rows I'll be getting. The mistake is somewhere within this loop, so any insight you can provide would be much appreciated.
1 2 3 4 5 6 7 8 9 10 11 12
while(!myfile.eof())
{
int a=0;
testVector.push_back ( vector<string>() );
for ( int i = 0; i < numberOfCols; i++ )
{
getline(myfile, line,',');
testVector[a].push_back(line);
}
a++;
}
Try your algorithm with a very small input file, and add 'cout << line << endl;' before each push_back(line), so you see what is being added to the vector. I believe the problem is that you aren't accounting for the fact that the last value in each line of a CSV does not end with a comma, but with a newline. I would recommend you use something like
getline(myfile, line);
and then tokenize the line into individual values. Also, keep in mind that you should account for quoted values in a csv. For example,
Val 1, 'this is Val 2, but with a comma', Val 3
is 3 columns, but may accidentally be parsed as 4.
Thanks for the suggestions- I was able to get things working. Any idea, though, why I seemed to have more luck using getline to grab the entire row and then tokenize it myself? That is, aside from the final value not ending in a comma?
I don't see how this: getline(myfile, line,','); will correctly extract the last column. The last column does not end with a comma ',' but with a newline '\n'.