1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| typedef struct{
int a;
char B;
float c;
} Structure_a;
typedef struct {
char * d;
Structure_a *e;
float f;
int nba;
short g;
} Structure_b;
void write_Structure_a(Structure_a const *p, FILE* f)
{
fprintf(f, "%d\n", p->a);
fprintf(f, "%d\n", p->B);
fprintf(f, "%f\n", p->c);
}
void read_Structure_a(Structure_a *p, FILE* f)
{
char s[100];
int i;
fgets(s, 100, f);
sscanf(s, "%d", &p->a);
fgets(s, 100, f);
sscanf(s, "%d", &i); /*Because C90 has no decimal format for chars*/
p->B = i;
fgets(s, 100, f);
sscanf(s, "%f", &p->c);
}
void write_Structure_b(Structure_b const *p, FILE* f)
{
int ix;
fprintf(f, "%d\n", p->d);
fprintf(f, "%d\n", p->nba);
for(ix=0 ; ix<p->nba ; ix++)
write_Structure_a(&p->e[ix], f);
fprintf(f, "%f\n", p->f);
fprintf(f, "%d\n", p->g);
}
void read_Structure_b(Structure_b *p, FILE* f)
{
char s[100];
int ix;
fgets(s, 100, f);
sscanf(s, "%d", &p->d);
fgets(s, 100, f);
sscanf(s, "%d", &p->nba);
p->e = malloc(sizeof(Structure_a) * p->nba); /*pour la gestion d'erreur on verra plus tard*/
for(ix=0 ; ix<p->nba ; ix++)
read_Structure_a(&p->e[ix], f);
fgets(s, 100, f);
sscanf(s, "%f", &p->f);
fgets(s, 100, f);
sscanf(s, "%hd", &p->g);
} |
Partager