0

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
william_
  • 852
  • 3
  • 15
  • Well the exception is the defined behavior of [`std::stoi`](https://en.cppreference.com/w/cpp/string/basic_string/stol) if it's presented with something that's not an "integer". – Some programmer dude Oct 31 '20 at 01:36
  • As for the reason behind the exception being thrown, I'm guessing it's because of [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) Your loop `while (file.good())` suffers from the exact same problem. – Some programmer dude Oct 31 '20 at 01:38
  • @Someprogrammerdude so i should be doing something like while (getline(file,number,',') && getline(file,number,',') ... ? – william_ Oct 31 '20 at 01:51
  • Your example doesn't have any commas or an `s`. Is that just an oversight when you added the data here? Is it failing on the first line or later? It would help to see the actual data it is failing on. – Retired Ninja Oct 31 '20 at 01:53
  • 2
    I would rather recommend you read a *full* line from the file (yes, with `while (getline(file, full_line))`) and then put that line into a `std::istringstream` that you then can parse with e.g. `getline(line_stream, number, ',')` – Some programmer dude Oct 31 '20 at 01:58
  • @Someprogrammerdude could i please more info i tried it like this in my edit post but got a new error as mentioned – william_ Oct 31 '20 at 02:19
  • please edit last edit – william_ Oct 31 '20 at 02:34
  • Is `raw` supposed to be the rest of the line? Then don't use the `','` as the separator. – Some programmer dude Oct 31 '20 at 02:41

0 Answers0