Reading/Writing a structure into a binary file

前端 未结 1 1391
礼貌的吻别
礼貌的吻别 2020-12-09 13:46

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 Es         


        
相关标签:
1条回答
  • 2020-12-09 14:27

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

    0 讨论(0)
提交回复
热议问题