I have a csv file with the pattern:
firstname, lastname, id
angelina, jolie, 247
... and so on...
I want to store the values in a Student
class, but the problem is that it takes the last word of the first line and it concatenate with the first word of the second line(idangelina
).The list has 5000 students to store in a Student
array and I don't know how to separate them.
The function is this:
int readFromCsv(string filePath, Student v[]){
string line;
ifstream csvfile;
csvfile.open(filePath);
int i = 0;
while ( !csvfile.eof() ){//loops until the end of the file
getline(csvfile, line, ',');//read a string until the next comma
v[i].setFirstName(line); //set each student
getline(csvfile, line, ',');
v[i].setLastName(line);
getline(csvfile, line, ',');
v[i].setId(line);
i += 1;
}
csvfile.close();
return i;
}