c++ Read from .csv file -
i have code supposed cout in console information .csv file;
while(file.good()) { getline(file, id, ','); cout << "id: " << id << " " ; getline(file, nome, ',') ; cout << "user: " << nome << " " ; getline(file, idade, ',') ; cout << "idade: " << idade << " " ; getline(file, genero, ' ') ; cout << "sexo: " << genero<< " " ; } and csv file has (when open notepad):
0,filipe,19,m 1,maria,20,f 2,walter,60,m whenever run program console display this:

my question why isn't program repeating cout messages in every line instead of in first 1
btw , nome name, idade age, , genero/sexo gender, forgot translate before creating post
you can follow this answer see many different ways process csv in c++.
in case, last call getline putting last field of first line , of remaining lines variable genero. because there no space delimiter found until end of file. try changing space character newline instead:
getline(file, genero, file.widen('\n')); or more succinctly:
getline(file, genero); in addition, check file.good() premature. last newline in file still in input stream until gets discarded next getline() call id. @ point end of file detected, check should based on that. can fix changing while test based on getline() call id (assuming each line formed).
while (getline(file, id, ',')) { cout << "id: " << id << " " ; getline(file, nome, ',') ; cout << "user: " << nome << " " ; getline(file, idade, ',') ; cout << "idade: " << idade << " " ; getline(file, genero); cout << "sexo: " << genero<< " " ; } for better error checking, should check result of each call getline().
Comments
Post a Comment