Help linked list overwrites string [closed]

3

The problem arises when I upload the data from a binary file to memory. I do not understand why the values of the string (Desc.) Of each product are overwritten (copying the description of the last product to all the previous ones) but the numerical values are correct. The program compiles, but when I show the list on the screen the result is:

ID      Desc.                          Cant.         Importe

12      Teclado                         12           1200

04      Teclado                         04           4000

15      Teclado                         15           1500

When it should be:

ID      Desc.                         Cant.          Importe

12      Silla                           12           1200

04      Mesa                            04           4000

15      Teclado                         15           1500

I pass the code:

template <typename T> struct Node {
     T info;  
     Node<T>* sig; 
};

struct Producto {
 int id;
 string descripcion;
 int len;
 int cantidad;
 long importe;
};

 template <typename T> Node<T>* add(Node<T>*& p, T x)
 {
 Node<T>* nuevo = new Node<T>();
 nuevo->info = x;
 nuevo->sig = NULL;
 if( p==NULL )
 {
    p = nuevo;
 }
 else
 {
    Node<T>* aux = p;
    while( aux->sig!=NULL )
    {
       aux = aux->sig;
    }
    aux->sig = nuevo;
 }
   return nuevo;
 }


Producto productoLeer(FILE* f) {
 Producto producto;
 producto.id = read<int>(f);
 producto.len = read<int>(f);

 for(int i=0; i<producto.len; i++) {
    producto.descripcion[i] = read<char>(f);
 }

 for(int j=producto.len ; j<25 ; j++) {
    producto.descripcion[j] = 32;
 }

 producto.cantidad = read<int>(f);
 producto.importe = read<long>(f);
 return producto;
}


 Node<Producto>* getLista() 
 {
 FILE* inventario = fopen("inventario.DAT", "r+b" );
 long size = fileSize<Producto>(inventario);

 Node<Producto>* listaProductos = NULL;

    for(int i=0; i<size ; i++) 
    {
       Producto producto = productoLeer(inventario);
       add<Producto>(listaProductos, producto);
    }

  showList(listaProductos);

  fclose(inventario);
  return listaProductos;
 }
    
asked by Agustin 23.04.2017 в 00:43
source

0 answers