I'm trying to read in a .csv file that looks like this:
Name,Place,Age,x,y
A,X,1,50,100
B,Y,2,-90,20
C,Z,3,0.4,80
...
Except there are 100 rows of data (plus the header).
I would like to read in the columns Name, Age, x and y and place them in an vector that looks like this:
Name = [Age, x, y]
and do this for all 100 rows (so 100 vectors).
I've tried searching the forum for help but the best help I could get was from c++ Skip first line of csv file , which I modified slightly just to print the Age, x, y.
ifstream data("data.csv");
if (!data.is_open())
{
exit(EXIT_FAILURE);
}
string str;
getline(data, str); // skip the first line
while (getline(data, str))
{
istringstream iss(str);
string token;
while (getline(iss, token, ','))
{
double Age_x_y = atof(token.c_str());
if (Age_x_y != 0) {
cout << Age_x_y << " ";
}
cout << endl;
}
}
This is nice if it was all I wanted to output, but I believe all the data is just stored as a double. I need the data stored in a vector (or something else like a structure) so that I can manipulate the entries. For example, I would like to work out x+y for each Name.
How can I extract the data in this way?
Any help would be greatly appreciated.