Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3021

Help reading txt in a linked list

$
0
0
I need help because my code saves the txt correctly but at the time of reading it, returns the linked list empty.

HTML Code:

#include "Empleado.h"



Empleado::Empleado() {
        cedula = "";
        nombre = "";
        apellidos = "";
        nacimiento = "";
        direccion = "";
        telefono = "";
}

Empleado::Empleado(string c, string n, string a, string na, string d, string t) {
        cedula = c;
        nombre = n;
        apellidos = a;
        nacimiento = na;
        direccion = d;
        telefono = t;
        f = new Fecha();
}

Empleado::~Empleado() {
}

// SETS & GETS
string Empleado::toString() {
        stringstream s;
        s << "=================| EMPLEADO |=================" << endl;
        s << "Cedula: " << cedula << endl;
        s << "Nombre: " << nombre << endl;
        s << "Apellidos: " << apellidos << endl;
        s << "Nacimiento(DD/MM/AAAA):" << f->
obtenerFecha(nacimiento) << endl;
        s << "Direccion: " << direccion << endl;
        s << "Telefono: " << telefono << endl;
        s << "==============================================" << endl;
        return s.str();
}


#include "ListaEmpleado.h"



ListaEmpleado::ListaEmpleado() {
        primero = NULL;
        cantidad = 0;
}

ListaEmpleado::~ListaEmpleado() {
        NodoEmpleado* actual = primero;
        while (actual != NULL) {
                NodoEmpleado* temp = actual->
getSig();
                delete actual;
                actual = temp;
        }
}

void ListaEmpleado::limpiar() {
        NodoEmpleado* actual = primero;
        while (primero) {
                primero = primero->getSig();
                delete actual;
        }
        actual = NULL;// o colocar delete actual
        primero = NULL;// o colocar delete primero

        cantidad = 0;
}

void ListaEmpleado::ingresarEmp(Empleado* emp1) {
        NodoEmpleado* actual;
        actual = primero;
        if (primero == NULL) {
                primero = new NodoEmpleado(NULL, emp1);
                cantidad++;
        }
        else {
                while (actual->getSig() != NULL) {
                        actual = actual->getSig();
                }
                actual->setSig(new NodoEmpleado(NULL, emp1));
                cantidad++;
        }
}


void ListaEmpleado::eliminarEmp(string ced) {
        NodoEmpleado* anterior = NULL;
        NodoEmpleado* actual = primero;
        while (actual && actual->getEmpleado()->getCedula() != ced) {
                anterior = actual;
                actual = actual->getSig();
        }
        if (!actual || actual->getEmpleado()->getCedula() != ced)//Si no lo encontro
                return;
        else {//Borrar el nodo
                if (!anterior)//si es el primer elemento
                        primero = actual->getSig();
                else//si es un elemento intermedio o el ultimo
                        anterior->setSig(actual->getSig());
                delete actual;
        }
}


bool ListaEmpleado::buscaEmpleado(string c) {
        NodoEmpleado* actual = primero;
        while (actual != NULL) {
                if (actual->getEmpleado()->getCedula() == c) {
                        return true;
                }
                else
                        actual = actual->getSig();
        }
        return false;
}

void ListaEmpleado::modificarEmpleadoNom(string ced, string nuevo) {
        NodoEmpleado* actual = primero;
        while (actual->getEmpleado()->getCedula() != ced) {
                actual = actual->getSig();
        }

        if (actual->getEmpleado()->getCedula() == ced) {
                actual->getEmpleado()->setNombre(nuevo);
        }
}

void ListaEmpleado::modificarEmpleadoApe(string ced, string nuevo) {
        NodoEmpleado* actual = primero;
        while (actual->getEmpleado()->getCedula() != ced) {
                actual = actual->getSig();
        }

        if (actual->getEmpleado()->getCedula() == ced) {
                actual->getEmpleado()->setApellidos(nuevo);
        }
}

void ListaEmpleado::modificarEmpleadoNac(string ced, string nuevo) {
        NodoEmpleado* actual = primero;
        while (actual->getEmpleado()->getCedula() != ced) {
                actual = actual->getSig();
        }

        if (actual->getEmpleado()->getCedula() == ced) {
                actual->getEmpleado()->setNacimiento(nuevo);
        }
}

void ListaEmpleado::modificarEmpleadoDir(string ced, string nuevo) {
        NodoEmpleado* actual = primero;
        while (actual->getEmpleado()->getCedula() != ced) {
                actual = actual->getSig();
        }

        if (actual->getEmpleado()->getCedula() == ced) {
                actual->getEmpleado()->setDireccion(nuevo);
        }

}

void ListaEmpleado::modificarEmpleadoTel(string ced, string nuevo) {
        NodoEmpleado* actual = primero;
        while (actual->getEmpleado()->getCedula() != ced) {
                actual = actual->getSig();
        }

        if (actual->getEmpleado()->getCedula() == ced) {
                actual->getEmpleado()->setTelefono(nuevo);
        }
}

Empleado* ListaEmpleado::getEmpleado(string ced) {
        NodoEmpleado* aux = primero;
        while (aux->getEmpleado()->getCedula() != ced) {
                aux = aux->getSig();
        }
        if (aux->getEmpleado()->getCedula() == ced) {
                return aux->getEmpleado();
        }
        return NULL;
}

void ListaEmpleado::guardar() {
        NodoEmpleado* aux = NULL;
        ofstream file("ReporteEmpleado.txt", ios::out);
        if (file.is_open()) {
                aux = primero;
                file << "Cantidad empleados: " << cantidad << endl;
                while (aux) {
                        string c = aux->
getEmpleado()->getCedula();
                        string n = aux->getEmpleado()->getNombre();
                        string a = aux->getEmpleado()->getApellidos();
                        string na = aux->getEmpleado()->getNacimiento();
                        string d = aux->getEmpleado()->getDireccion();
                        string t = aux->getEmpleado()->getTelefono();

                        file << c << endl;
                        file << n << endl;
                        file << a << endl;
                        file << na << endl;
                        file << d << endl;
                        file << t << endl;

                        aux = aux->
getSig();
                }
                file.close();
        }
}
void ListaEmpleado::cargar() {
        NodoEmpleado* aux = NULL;
        ifstream file("ReporteEmpleado.txt", ios::in);
        if (file.is_open()) {
                aux = primero;
                int cantidad = 0;
                int i = 0;
                while (aux && file.eof()) {
                        //Empleado* emp;
                        string c ;
                        string n ;
                        string a ;
                        string na;
                        string d ;
                        string t ;
                       
                        file>>c;
                        file>>n;
                        file>>a;
                        file>>na;
                        file>>d;
                        file>>t;
                       
                        //emp->setCedula(c);
                        //emp->setNombre(n);
                        //emp->setApellidos(a);
                        //emp->setNacimiento(na);
                        //emp->setDireccion(d);
                        //emp->setTelefono(t);

                        ingresarEmp(new Empleado(c,n,a,na,d,t));
                        cantidad++;

                        aux = aux->getSig();
                }
                file.close();
        }
}

string ListaEmpleado::toString() {
        stringstream s;
        NodoEmpleado* aux = primero;
        s << "===================== LISTA DE EMPLEADOS ======================" << endl;
        while (aux) {
                s << aux->
toString();
                aux = aux->getSig();
        }
        s << "===============================================================" << endl;
        return s.str();
}


Viewing all articles
Browse latest Browse all 3021