Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct viaje {
- char *identificador;
- char *ciudadDestino;
- char *hotel;
- int numeroNoches;
- char *tipoTransporte;
- float precioAlojamiento;
- float precioDesplazamiento;
- };
- struct cliente {
- char *dni;
- char *nombre;
- char *apellidos;
- char *direccion;
- int totalViajes;
- struct viaje *viajes;
- } *clientes;
- for (i = 0; i < MAX_TAM_CLIENTES; i++) {
- fwrite(&clientes[i], sizeof(struct cliente)-(sizeof(struct viaje)*MAX_TAM_VIAJES_CLIENTE), 1, fp_guardarCargarEstado);
- for (j = 0; j < clientes[i].totalViajes; j++) {
- fwrite(&clientes[i].viajes[j], sizeof(struct viaje), 1, fp_guardarCargarEstado);
- }
- }
- MAX_TAM_CLIENTES is a define. Is the max size of array clientes
- MAX_TAM_VIAJES_CLIENTE is a define. Is the max size of array viajes inside one clientes
- for (i = 0; i < MAX_TAM_CLIENTES; i++) {
- clientes = (struct cliente *)realloc(clientes, (totalClientes+1)*sizeof(struct cliente));
- clientes[totalClientes].dni = (char *)malloc((MAX_TAM_DNI+1)*sizeof(char));
- clientes[totalClientes].nombre = (char *)malloc((MAX_TAM_NOMBRE+1)*sizeof(char));
- clientes[totalClientes].apellidos = (char *)malloc((MAX_TAM_APELLIDOS+1)*sizeof(char));
- clientes[totalClientes].direccion = (char *)malloc((MAX_TAM_DIRECCION+1)*sizeof(char));
- fread(&clientes[i], sizeof(struct cliente)-(sizeof(struct viaje)*MAX_TAM_VIAJES_CLIENTE), 1, fp_guardarCargarEstado);
- for (j = 0; j < clientes[i].totalViajes; j++) {
- clientes = (struct viaje *)realloc(viajes, (clientes[i].totalViajes+1)*sizeof(struct viaje));
- clientes[i].viajes[j].identificador = (char *)malloc((MAX_TAM_IDENTIFICADOR+1)*sizeof(char));
- clientes[i].viajes[j].ciudadDestino = (char *)malloc((MAX_TAM_CIUDAD_DESTINO+1)*sizeof(char));
- clientes[i].viajes[j].hotel = (char *)malloc((MAX_TAM_HOTEL+1)*sizeof(char));
- clientes[i].viajes[j].tipoTransporte = (char *)malloc((MAX_TAM_TIPO_TRANSPORTE+1)*sizeof(char));
- fread(&clientes[i].viajes[j], sizeof(struct viaje), 1, fp_guardarCargarEstado);
- }
- }
- MAX_TAM_* is a define. It say maximum size of each field
Add Comment
Please, Sign In to add comment