|
-
April 18th, 2016, 07:17 PM
#1
Help reading txt in a linked list
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();
}
-
April 19th, 2016, 10:30 AM
#2
Re: Help reading txt in a linked list
That's more code than I feel like wading through, but did you try the debugger?
-
April 19th, 2016, 12:23 PM
#3
Re: Help reading txt in a linked list
Well, I agree with GCDEF.
Besides:
 Originally Posted by KArroyo
... my code saves the txt correctly but at the time of reading it, returns the linked list empty.
How did you check the data was saved correctly?
Did you try to investigate which part: read in the data or fill in the "linked list" failed?
Victor Nijegorodov
-
April 19th, 2016, 02:20 PM
#4
Re: Help reading txt in a linked list
When posting code, please use code tags rather than HTML tags. Go Advanced, select the code and click '#'.
You haven't provided the .h files.
A couple of comments
One version of empleado constructor sets f using dynamic memory allocation and the default constructor doesn't set f?
As dynamic memory is used with empleado, the empleado class destructor should free the allocated memory.
Code:
while (aux && file.eof()) {
Do you mean this? The while loop will only process if the file is at the end??? Don't you mean
Code:
while (aux && !file.eof()) {
Also why the test on aux? Shouldn't the linked list be extended for each entry read from the file? So why not simply
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|