jeudi 25 avril 2019

How to properly apply this nested ifstream?

I want to use an ifstream function that calls another ifstream function inside it, but I have no idea why it does not work. The ifstream called first works fine, but the second one does not. This is the first one:

ifstream archivo("civilizaciones.txt");
if(archivo.is_open()){
    while(!archivo.eof()){
        string name;
        Civilizacion a;

        getline(archivo, name);

        if(archivo.eof()){ break; }
        a.setName(name);
        arreglo.push_back(a);
        c.recuperarAld(name);
    }
}

c.recuperarAld(name) Calls the following function in a class with object c:

void Civilizacion::recuperarAld(string name)
{
    ifstream aldeanoss(name + ".txt");
    if(aldeanoss.is_open()){
        while(!aldeanoss.eof()){
            Aldeano a;
            string linea;

            getline(aldeanoss,linea);

            if(aldeanoss.eof()){break;}

            a.setNombre(linea);

            getline(aldeanoss, linea);
            a.setEdad(stoul(linea));

            getline(aldeanoss, linea);
            a.setGenero(linea);

            getline(aldeanoss, linea);
            a.setSalud(stoi(linea));

            aldeanos.push_back(a);
        }
    }
}

But this second one does not seem to work, how can I fix it?

Aucun commentaire:

Enregistrer un commentaire