I'm trying to read info from a txt file but something isnt working out. I need to read each line and save that in a variable that belongs to the class Bilhete.
Alexandre
20/12/2015
1,2,3,4
Luis
30/10/1990
3,4,5,6
This is my txt.
This represents a ticket(bilhete in portuguese): - name of the owner - expirity date - The competitions that I have access to. Each number represents the id of the competition. There is a competition class in my project but I dont think it is very important for this question
void Campeonato::readFileBilhetes(string filename) {
ifstream ficheiro_leitura(filename.c_str());
vector<Prova*> vecprovas = calendario->getProvas();
vector <int> indices; //vector with competitions id
if(!ficheiro_leitura)
throw ErroNoFicheiro(filename);
else{
string dono, data, provasNoBilhete, vendido; //owner, date, competitions in the ticket,
while (!ficheiro_leitura.eof()){
getline(ficheiro_leitura, dono);
getline(ficheiro_leitura, data);
getline(ficheiro_leitura, provasNoBilhete);
stringstream dataSs;
date d;
dataSs << data;
int dia, mes, ano; //day, month, //year
char tmp;
dataSs >> dia >> tmp >> mes >> tmp >> ano;
if(dia < 1 || dia > 31|| mes < 1 || mes > 12 || ano < 1 || (dia > 28 && mes == 2) || (dia > 30 && mes == 4) || (dia > 30 && mes == 6) || (dia > 30 && mes == 9) || (dia > 30 && mes == 11)){
cout << "Data invalida!";
}
else{
d.dia = dia; //day
d.mes = mes; //month
d.ano = ano; //year
}
string s1 = "";
string s2= "";
stringstream provasS;
int i = 0;
while (provasNoBilhete[i] != '\n' && provasNoBilhete[i] != '\0'){
if(provasNoBilhete[i] == ','){
int indice;
provasS.clear();
provasS.str("");
provasS.str(s2);
provasS >> indice;
indices.push_back(indice);
}
else
provasS << provasNoBilhete[i];
++i;
Bilhete *bi1 = new Bilhete(d, dono, indices);
inserirBilhete(*bi1); //insert
}
}
}
}
How do I correcly save those values?
Best regards