I had a problem with the pointers. I wanna read a binary file with a function, and then, use the read data in the main. The problem is that I had to pass a pointer to array of struct to can use the data in main.
The code is:
#define TMOLDEO 8
#define TAM 41
struct fichpiezas{
int codPieza;
float dimPieza;
float costePieza[TMOLDEO];
};
int leer_fichero(struct fichpiezas *vpiezas[]);
int main(int argc, const char * argv[]) {
struct fichpiezas vpiezas[TAM];
leer_fichero(&vpiezas);
for(int i = 0; sizeof(vpiezas)/sizeof(struct fichpiezas); i++){
printf("Codigo pieza : %d\n", vpiezas[i].codPieza);
}
return 0;
}
int leer_fichero (struct fichpiezas *vpiezas[]){
FILE *fich;
struct fichpiezas pieza;
int error_dev = 0, i = 0;
if ((fich = fopen("piezas.bin", "rb")) == NULL){
printf ( "Error en apertura del fichero para lectura \n " );
error_dev = 1;
} else{
//Bucle mientras no sea fin de fichero
while (! feof(fich)){
fread (&pieza, sizeof(pieza), 1, fich);
vpiezas[i] = &pieza;
i++;
}
fclose (fich);
}
return error_dev;
}