Populating 2x2 Vector

Hey all,

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++;
        }


Thanks!
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.

--Rollie
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?
Last edited on
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'.

I'd be tempted to do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

int main()
{
	std::vector<std::vector<std::string> > testVector;
	std::ifstream myfile("test.txt");

	// Line by line
	std::string line;
	while(std::getline(myfile, line))
	{
		// convert line into an input stream
		std::istringstream iss(line);
		testVector.push_back(std::vector<std::string>());

		// Column by column
		std::string column;
		while(std::getline(iss, column, ','))
		{
			// add to last element of testVector (back())
			testVector.back().push_back(column);
		}
	}

	for(size_t i(0); i < testVector.size(); ++i)
	{
		for(size_t j(0); j < testVector.size(); ++j)
		{
			std::cout << "testVector[" << i << "][" << j << "] ="
				<< testVector[i][j] << std::endl;
		}
	}
	return 0;
}
Last edited on
Galik, you're completely correct. However, due to the output I was getting, I was wondering if maybe there was some other issue causing problems.

Rollie and Galik, thanks for taking the time to help- I appreciate it!
Topic archived. No new replies allowed.