I need help trying to read from a csv file.
the layout of my csv is the following
1 bob 1 2 11 3 4 5 6 6
2 jane 2 2 11 3 4 5 6 6
and so on
this is how I was trying to read from it
std::ifstream file;
Data d;
std::vector<Data> values;
std::string number, name;
file.open("Data-Organisation-Test1.csv");
while (file.good())
{
getline(file,number,',');
getline(file,name,',');
//std::cout<< typeid(number).name();
int num = stoi(number);
d.num = num;
d.name = name;
// go through all fields
values.push_back(d);
}
problems I am facing:
i get the following error when using stoi()
terminate called after throwing an instance of 'std::invalid_argument'
what(): stoi
when I output the datatype of the variable number it returns 's' secondly because of this im not sure if i am going about it right of what i am trying to achieve.
this is what i am trying to achieve:
struct Data{
int number;
std::string name;
// ...
}
read from csv file and get reach column and place into struct ie:
getline(file,number,',');
getline(file,name,',');
d.number = number;
d.name = name;
...
I am also getting a return value of 3
EDIT: this is the closest i have gotten to it to work
while (!inFile.eof()) {
getline ( inFile, number, ',' );
getline ( inFile, name, ',' );
getline ( inFile, mark, ',' );
getline ( inFile, raw, ',' );
std::cout << "number: " << number << std::endl;
std::cout << "name: " << name<< std::endl;
std::cout << "mark: " << mark<< std::endl;
std::cout << "raw: " << raw<< std::endl;
}
this outputs correct for the first row and every other row is off by one ie:
output:
number: 1
name: A
mark: 2
raw: 13
2
number: b
mark: 3
raw: 14
3
weight: c
number: 4
name: 11