Hi, I hope someone can clarify me how can I read from a file using a pointer that I need to read all the pets that I am adding through a function. Before reading from a file, everything worked perfectly, I was able to print all the records I added previously. I am not able to do so with files. This is my code:
Structure:
Code:
typedef struct {
unsigned int dia;
unsigned int mes;
unsigned int anio;
} fecha;
typedef struct mascota {
char nombre_mascota[100];
char tipo_mascota[100];
char raza[100];
char sexo[100];
char observaciones[1024];
char dolencia[1024];
fecha nacimiento;
struct mascota *siguiente;
} tmascota;
tmascota *primero=(tmascota*) NULL;
tmascota *ultimo=(tmascota*) NULL;
The function to print all the records added:
Code:
void listar_mascotas(tmascota *ptr) {
FILE *file;
int cont=0;
ptr=primero;
file=fopen("gestion_veterinaria.dat", "rb");
int num_leidos=1;
while (ptr!=NULL && num_leidos) {
num_leidos=fread(ptr, sizeof(tmascota),1, file);
if (num_leidos) {
printf("\n\n%s \nNombre de la Mascota: %s \nFecha de Nacimiento: %d/%d/%d\nTipo: %s \n"
"Raza: %s \nSexo: %s \nObservaciones: %s \nDolencia: %s\n",
ptr->nombre_mascota, ptr->nombre_mascota, ptr->nacimiento.dia, ptr->nacimiento.mes,
ptr->nacimiento.anio, ptr->tipo_mascota, ptr->raza, ptr->sexo, ptr->observaciones, ptr->dolencia);
ptr=ptr->siguiente;
cont++;
}
}
fclose(file);
}
I have problems with: num_leidos=fread(ptr, sizeof(tmascota),1, file);
Can someone see what is wrong? As I said, before file management everything worked perfectly.