Reading a csv file and store the values in c++ -
i have csv file pattern: firstname, lastname, id angelina, jolie, 247 , on. want store values in student class, problem takes last word of first line , concatenate first word of second line(idangelina).the list has 5000 students store in student array , don't know how separate them. function this:
int readfromcsv(string filepath, student v[]){ string line; ifstream csvfile; csvfile.open(filepath); int = 0; while ( !csvfile.eof() ){//loops until end of file getline(csvfile, line, ',');//read string until next comma v[i].setfirstname(line); //set each student getline(csvfile, line, ','); v[i].setlastname(line); getline(csvfile, line, ','); v[i].setid(line); += 1; } csvfile.close(); return i; }
for last field in 1 line should use
getline(csvfile, line); v[i].setid(line);
instead of
getline(csvfile, line, ','); v[i].setid(line);
because last field don't want read until next comma until end of line.
Comments
Post a Comment