vendredi 8 mai 2015

How to read array of struct of a bin file in C

I have this code that works for me:

 fp = fopen("person.dat", "rb");
    struct Person{
        int pnummer;
        char name[20];
        float lenght;
        float weight;
    };
    struct Person*object=malloc(sizeof(struct Person));
    fread(object, sizeof(struct Person), 1, fp);
    printf("%s\n%d\n%f\n%f\n",object->name,object->pnummer,object->lenght,object->weight);

Output: Andreas 660311 181.000000 82.000000

I would like to read an array of this struct. So that i can have 10 persons. How do I write the last 3 lines?

typedef struct{
    int pnummer;
    char name[20];
    float lenght;
    float weight;
}Person;
Person p_lista[10];

Aucun commentaire:

Enregistrer un commentaire