Guest User

Untitled

a guest
Mar 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. struct viaje {
  2. char *identificador;
  3. char *ciudadDestino;
  4. char *hotel;
  5. int numeroNoches;
  6. char *tipoTransporte;
  7. float precioAlojamiento;
  8. float precioDesplazamiento;
  9. };
  10.  
  11. struct cliente {
  12. char *dni;
  13. char *nombre;
  14. char *apellidos;
  15. char *direccion;
  16. int totalViajes;
  17. struct viaje *viajes;
  18. } *clientes;
  19.  
  20. for (i = 0; i < MAX_TAM_CLIENTES; i++) {
  21. fwrite(&clientes[i], sizeof(struct cliente)-(sizeof(struct viaje)*MAX_TAM_VIAJES_CLIENTE), 1, fp_guardarCargarEstado);
  22.  
  23. for (j = 0; j < clientes[i].totalViajes; j++) {
  24. fwrite(&clientes[i].viajes[j], sizeof(struct viaje), 1, fp_guardarCargarEstado);
  25. }
  26. }
  27.  
  28. MAX_TAM_CLIENTES is a define. Is the max size of array clientes
  29. MAX_TAM_VIAJES_CLIENTE is a define. Is the max size of array viajes inside one clientes
  30.  
  31. for (i = 0; i < MAX_TAM_CLIENTES; i++) {
  32. clientes = (struct cliente *)realloc(clientes, (totalClientes+1)*sizeof(struct cliente));
  33. clientes[totalClientes].dni = (char *)malloc((MAX_TAM_DNI+1)*sizeof(char));
  34. clientes[totalClientes].nombre = (char *)malloc((MAX_TAM_NOMBRE+1)*sizeof(char));
  35. clientes[totalClientes].apellidos = (char *)malloc((MAX_TAM_APELLIDOS+1)*sizeof(char));
  36. clientes[totalClientes].direccion = (char *)malloc((MAX_TAM_DIRECCION+1)*sizeof(char));
  37.  
  38. fread(&clientes[i], sizeof(struct cliente)-(sizeof(struct viaje)*MAX_TAM_VIAJES_CLIENTE), 1, fp_guardarCargarEstado);
  39.  
  40. for (j = 0; j < clientes[i].totalViajes; j++) {
  41. clientes = (struct viaje *)realloc(viajes, (clientes[i].totalViajes+1)*sizeof(struct viaje));
  42. clientes[i].viajes[j].identificador = (char *)malloc((MAX_TAM_IDENTIFICADOR+1)*sizeof(char));
  43. clientes[i].viajes[j].ciudadDestino = (char *)malloc((MAX_TAM_CIUDAD_DESTINO+1)*sizeof(char));
  44. clientes[i].viajes[j].hotel = (char *)malloc((MAX_TAM_HOTEL+1)*sizeof(char));
  45. clientes[i].viajes[j].tipoTransporte = (char *)malloc((MAX_TAM_TIPO_TRANSPORTE+1)*sizeof(char));
  46.  
  47. fread(&clientes[i].viajes[j], sizeof(struct viaje), 1, fp_guardarCargarEstado);
  48. }
  49. }
  50.  
  51. MAX_TAM_* is a define. It say maximum size of each field
Add Comment
Please, Sign In to add comment