| Код | #include <stdio.h> #include <string.h> #define N 5
struct swed { char fio[256]; char pl; int god; };
int main() { int i; struct swed d; int t, k; FILE *f = fopen("f.txt", "wb"); if (!f) { fprintf(stderr, "Failed to open file for writing\n"); return 1; } for(i = 0; i < N; ++i) { memset(&d, 0, sizeof(d)); puts("famaly: "); scanf("%255c", d.fio); puts("pol: "); scanf("%c", &d.pl); puts("vozrast: "); scanf("%d", &d.god); fwrite(d, sizeof(d), 1, f); } fclose(f); f = fopen("f.txt", "rb"); if (!f) { fprintf(stderr, "Failed to open file for reading\n"); return 1; } t = 0; k = 0; for(i = 0; i < N; ++i) { fread(&d, sizeof(d), 1, f); if (d.pl == 'm') { printf("%s\n", d.fio); t += d.god; ++k; } } if (k > 0) printf("s=%d\n", t/k); fclose(f); return 0; } |
|