|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Jul 2008
Messaggi: 24
|
[C++] Aiuto input da file
ciao a tutti
ho un problema con una funzione che legge e immagazzina in un vector dati presi in input da un generico txt Codice:
void ReadEmploye(const string& filepath, vector<Employ> &employs)
{
fstream data;
data.open(filepath.c_str(),ios::in);
string str;
while(data)
{
getline(data,str);
int pos=str.find_first_of(",",0);
string FName = str.substr(0,pos);
int pos1 = str.find_first_of(",",pos+1);
string LName = str.substr(pos+1,pos1-1-pos);
string sal = str.substr(pos1+1,str.size()-pos1);
double salary = atof(sal.c_str());
Employ obj(FName, LName, salary);
employs.push_back(obj);
}
data.close();
}
firstName1 LastName1 salary1 firstName2 LastName2 salary2 firstName3 LastName3 salary3 firstName3 LastName3 salary3 Suggerimenti?????? |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
L'eof viene generato solo dopo la prima lettura fallita.
Quindi metti un controllo sul failbit dopo la getline. Codice:
if(data.fail()) break; |
|
|
|
|
|
#3 |
|
Junior Member
Iscritto dal: Jul 2008
Messaggi: 24
|
grazie mille!!!!
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 06:51.



















