I am running a program with 3 structures and what I am doing to read/write in the binary file is the following:
struct Medico
{
    int Id_Doctor;
    int Estado;
    char Nombre[60];
    char Clave_Acceso[20];
    char Especialidad[40]; 
    struct Medico *next;
};
typedef struct Medico *Medicazos;
typedef struct Medico Meds;
Medicazos Nuevo;
FILE *Archivaldo;
char especialida[40], password[20];
char nombre_doc[60];
int estado_doc, id_doc;
Archivaldo=fopen("md.dat", "rb");
fclose(Archivaldo);
if((Archivaldo=fopen("md.dat", "rb"))==NULL)
{
    printf("No se pudo abrir el archivo de Medicos\n"); //couldnt open file msg
    exit(1);
}
rewind(Archivaldo);
while(!feof(Archivaldo))
{
    if(*Inicio != NULL)  //read from file and write in struct
    {
        Nuevo = (Medicazos) malloc (sizeof(Meds)); //new=pointer
        fread(&id_doc, sizeof(int), 1, Archivaldo);
        fread(nombre_doc, sizeof(char), sizeof(nombre_doc), Archivaldo);
        fread(password, sizeof(char), 20 , Archivaldo);
        fread(especialida, sizeof(char), 40, Archivaldo);
        fread(&estado_doc, sizeof(int), 1, Archivaldo);
        Nuevo->Id_Doctor=id_doc; ///copies data in structure
        strcpy(Nuevo -> Nombre , nombre_doc);
        strcpy(Nuevo -> Clave_Acceso, password);
        strcpy(Nuevo -> Especialidad, especialida);
        Nuevo-> Estado = estado_doc;
        printf("---------------------------------\n"); //display info
        printf("ID: %d\n", id_doc);
        printf("\nDoctor: ");
        puts(nombre_doc);
        printf("\nPassword: ");
        puts(password);
        printf("\nEspecialidad: ");
        puts(especialida);
        printf("\nEstado: ");
        if(estado_doc==1)
            puts("Activo\n");
        else
            puts("Inactivo\n");
        Nuevo-> next = *Inicio;
        *Inicio = Nuevo;
     }
     else 
     {
         *Inicio = (Medicazos)malloc(sizeof(Meds));
         fread(&id_doc, sizeof(int), 1, Archivaldo);
         fread(nombre_doc, sizeof(char), sizeof(nombre_doc), Archivaldo);
         fread(password, sizeof(char), 20 , Archivaldo);
         fread(especialida, sizeof(char), 40, Archivaldo);
         fread(&estado_doc, sizeof(int), 1, Archivaldo);
         (*Inicio)->Id_Doctor=id_doc;
         strcpy((*Inicio) -> Nombre , nombre_doc);
         strcpy((*Inicio) -> Clave_Acceso, password);
         strcpy((*Inicio) -> Especialidad, especialida);
         (*Inicio)-> Estado = estado_doc;
         printf("--------------------------------\n"); //display info
         printf("ID: %d\n", id_doc);
         printf("\nDoctor: ");
         puts(nombre_doc);
         printf("\nPassword: ");
         puts(password);
         printf("\nEspecialidad: ");
         puts(especialida);
         printf("\nEstado: ");
         if(estado_doc==1)
             puts("Activo\n");
         else
             puts("Inactivo\n");
         (*Inicio) -> next = NULL;
     }
 }
 fclose(Archivaldo);
Is there any way to do simplify this?
EDIT: Also, when I try to display the files someone mentioned me not to use feof while for reading/writing. I dont recall why. What could be used instead?
I'm assuming your struct looks like this:
struct Medicazos
{
  char Nombre[60];
  char Clave_Acceso[20];
  char Especialidad[40];
  int Id_Doctor;
  int Estado;
}
You can read/write/copy this guy around as a single unit. There's no need to do piecemeal access until you're ready to actually use the values.
struct Medicazos m = {"Bob", "Password", "Feet", 123, 456};
FILE* f = fopen(...);
fwrite(&m, sizeof(struct Medicazos), 1, f);
And the same (but backward) for fread.
(By the way, your capitalized variable names are killing me.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With