Failure to save data in a Dynamic Structure

1

I want to create a structure that grows in size when registering a new data.
It saves me the first three data well without any problem, when entering a fourth data the first data is deleted until entering a seventh data the second one is deleted.

#include<string.h>
#include<stdio.h>
#include<stdlib.h>

typedef struct{
  char *nombre;
  char *apellido;
}datos;

datos *dato;

void memDatos(int *tmx);
void reDatos(int *x);
void delSpace(char word[50]);
void memDinamic(char aux[50],int w, int d);
void editarDatos(int tmx);

int main(){
  int op;
  char key;
  int tmx = 0;
  memDatos(&tmx);// asignar tamanio de la estructura

  do{
     do{
        printf("\n  Digite un Comando;\n");
        printf("(1) Registrar Datos\n");
        printf("(2) Iniciar Busqueda\n");
        printf("(3) Editar Datos\n >>> ");
        scanf("%d",&op);
     }while(op < 1 || op > 3);

     switch(op){
        case 1: reDatos(&tmx); break;
    //  case 2: busqueda(tmx); break;
        case 3: editarDatos(tmx); break;
     }

     printf("\nTarea Finalizada\nDesea continuar (S/N) ");
     fflush(stdin);
     scanf("%c",&key);
  }while(key == 's' || key == 'S');


  system("pause>null");
  return 0;
}

void memDatos(int *tmx){

 dato = (datos*)malloc((*tmx+1)*sizeof(datos));// se registra el tamanio
 if(dato == NULL) exit(1);

}

void reDatos(int *x){  
 char auxiliar[50];
 int d=1;

  printf("Nombre?: "); fflush(stdin);
  fgets(auxiliar, 50, stdin);
  delSpace(auxiliar); // eliminar el salto de linea de fgets
  memDinamic(auxiliar,*x,d); d++; // asignar el tamanio adecuado para nombre
  printf("Apellido?: "); fflush(stdin);
  fgets(auxiliar, 50, stdin); 
  delSpace(auxiliar); memDinamic(auxiliar,*x,d);// x es la posicion a acceder en la estructura
  (*x)++; // incrementar el tamanio de estructura
}

void delSpace(char word[50]){  
 int i;  
 for(i = 0; i < 50; i++){
    if(word[i] == '\n'){  
        word[i] = '
#include<string.h>
#include<stdio.h>
#include<stdlib.h>

typedef struct{
  char *nombre;
  char *apellido;
}datos;

datos *dato;

void memDatos(int *tmx);
void reDatos(int *x);
void delSpace(char word[50]);
void memDinamic(char aux[50],int w, int d);
void editarDatos(int tmx);

int main(){
  int op;
  char key;
  int tmx = 0;
  memDatos(&tmx);// asignar tamanio de la estructura

  do{
     do{
        printf("\n  Digite un Comando;\n");
        printf("(1) Registrar Datos\n");
        printf("(2) Iniciar Busqueda\n");
        printf("(3) Editar Datos\n >>> ");
        scanf("%d",&op);
     }while(op < 1 || op > 3);

     switch(op){
        case 1: reDatos(&tmx); break;
    //  case 2: busqueda(tmx); break;
        case 3: editarDatos(tmx); break;
     }

     printf("\nTarea Finalizada\nDesea continuar (S/N) ");
     fflush(stdin);
     scanf("%c",&key);
  }while(key == 's' || key == 'S');


  system("pause>null");
  return 0;
}

void memDatos(int *tmx){

 dato = (datos*)malloc((*tmx+1)*sizeof(datos));// se registra el tamanio
 if(dato == NULL) exit(1);

}

void reDatos(int *x){  
 char auxiliar[50];
 int d=1;

  printf("Nombre?: "); fflush(stdin);
  fgets(auxiliar, 50, stdin);
  delSpace(auxiliar); // eliminar el salto de linea de fgets
  memDinamic(auxiliar,*x,d); d++; // asignar el tamanio adecuado para nombre
  printf("Apellido?: "); fflush(stdin);
  fgets(auxiliar, 50, stdin); 
  delSpace(auxiliar); memDinamic(auxiliar,*x,d);// x es la posicion a acceder en la estructura
  (*x)++; // incrementar el tamanio de estructura
}

void delSpace(char word[50]){  
 int i;  
 for(i = 0; i < 50; i++){
    if(word[i] == '\n'){  
        word[i] = '%pre%';  
      }  
  }  
}

void memDinamic(char aux[50],int x, int d){
  switch(d){
    case 1: dato[x].nombre = (char*)malloc((strlen(aux)+1)*sizeof(char));
            strcpy(dato[x].nombre,aux); break;
    default: dato[x].apellido = (char*)malloc((strlen(aux)+1)*sizeof(char));  
            strcpy(dato[x].apellido,aux); break;
 }

}

void editarDatos(int tmx){  
  int i;  
  puts("Usuarios registrados, seleccione alguno para continuar\n");  
  for(i = 0; i < tmx; i++){  
      printf("(%i) %s %s\n",i+1,dato[i].nombre,dato[i].apellido);  
   }  
}
'; } } } void memDinamic(char aux[50],int x, int d){ switch(d){ case 1: dato[x].nombre = (char*)malloc((strlen(aux)+1)*sizeof(char)); strcpy(dato[x].nombre,aux); break; default: dato[x].apellido = (char*)malloc((strlen(aux)+1)*sizeof(char)); strcpy(dato[x].apellido,aux); break; } } void editarDatos(int tmx){ int i; puts("Usuarios registrados, seleccione alguno para continuar\n"); for(i = 0; i < tmx; i++){ printf("(%i) %s %s\n",i+1,dato[i].nombre,dato[i].apellido); } }
    
asked by thomasnx 15.11.2018 в 22:13
source

1 answer

0

Look what you do:

void memDatos( int *tmx ){
  dato = (datos*)malloc((*tmx+1)*sizeof(datos));// se registra el tamanio
  if(dato == NULL) exit(1);
}

You call that function only once like this: memDatos(&tmx);

With that, you reserve memory for 1 single instance of your struct datos .

However, then you do:

void memDinamic(char aux[50],int x, int d){
  switch(d){
  case 1:
    dato[x].nombre = (char*)malloc((strlen(aux)+1)*sizeof(char));
    strcpy(dato[x].nombre,aux);
    break;

    default:
      dato[x].apellido = (char*)malloc((strlen(aux)+1)*sizeof(char));  
      strcpy(dato[x].apellido,aux);
      break;
   }
}

Look at what's important: you're dato[x] .

That's your problem: you treat datos as if it were a training or array in size x ... when in fact is in size fixed 1 .

One possible solution would be:

void reDatos(int *x){  
  char auxiliar[50];
  int d=1;

  if( *x ) {
    datos = (struct datos *)realloc( datos, sizeof( struct datos ) * ( ( *x ) + 1 ) );
    if( !datos ) exit( 1 );
  }

  printf("Nombre?: "); fflush(stdin);
  fgets(auxiliar, 50, stdin);
  delSpace(auxiliar); // eliminar el salto de linea de fgets
  memDinamic(auxiliar,*x,d); d++; // asignar el tamanio adecuado para nombre
  printf("Apellido?: "); fflush(stdin);
  fgets(auxiliar, 50, stdin); 
  delSpace(auxiliar); memDinamic(auxiliar,*x,d);// x es la posicion a acceder en la estructura
  (*x)++; // incrementar el tamanio de estructura
}

With that change, before adding elements, we resized your training , adding space for 1 more element.

Note : I have not tried it, but, except for some minimal details, it should work.

    
answered by 16.11.2018 в 07:41