Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

c - Reading/Writing a structure into a binary file

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
"); //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("---------------------------------
"); //display info
        printf("ID: %d
", id_doc);
        printf("
Doctor: ");
        puts(nombre_doc);
        printf("
Password: ");
        puts(password);
        printf("
Especialidad: ");
        puts(especialida);
        printf("
Estado: ");
        if(estado_doc==1)
            puts("Activo
");
        else
            puts("Inactivo
");
        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("--------------------------------
"); //display info
         printf("ID: %d
", id_doc);
         printf("
Doctor: ");
         puts(nombre_doc);
         printf("
Password: ");
         puts(password);
         printf("
Especialidad: ");
         puts(especialida);
         printf("
Estado: ");
         if(estado_doc==1)
             puts("Activo
");
         else
             puts("Inactivo
");
         (*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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...