Ecco qua.
#include <stdio.h>
#include <stdlib.h>
struct S
{
float a;
double b;
char c1, c2, c3;
short int d;
char c4;
long int e;
};
int main() {
FILE *fp;
float a = 1;
double b = 2;
char c1 = 'a', c2 = 'b', c3 = 'c';
short int d = 3;
char c4 = 'd';
long int e = 4;
S s = {11.f, 22., 'A','B','C', 33, 'D', 44l};
printf("Writing ...\n");
fp = fopen("es17.dat", "w");
fwrite(&a, sizeof(float), 1, fp);
printf("a: %f\n", a);
fwrite(&b, sizeof(double), 1, fp);
printf("b: %f\n", b);
fwrite(&c1, sizeof(char), 1, fp);
printf("c1: %c\n", c1);
fwrite(&c2, sizeof(char), 1, fp);
printf("c2: %c\n", c2);
fwrite(&c3, sizeof(char), 1, fp);
printf("c3: %c\n", c3);
fwrite(&d, sizeof(short int), 1, fp);
printf("d: %hd\n", d);
fwrite(&c4, sizeof(char), 1, fp);
printf("c4: %c\n", c4);
fwrite(&e, sizeof(long int), 1, fp);
printf("e: %ld\n", e);
fwrite(&s, sizeof(S), 1, fp);
printf("a: %f\n", s.a);
printf("s: %ld\n", s.e);
fclose(fp);
printf("... done\n");
printf("Reading ...\n");
fp = fopen("es17.dat", "r");
fread(&a, sizeof(float), 1, fp);
printf("a: %f\n", a);
fread(&b, sizeof(double), 1, fp);
printf("b: %f\n", b);
fread(&c1, sizeof(char), 1, fp);
printf("c1: %c\n", c1);
fread(&c2, sizeof(char), 1, fp);
printf("c2: %c\n", c2);
fread(&c3, sizeof(char), 1, fp);
printf("c3: %c\n", c3);
// fseek(fp, 2, 1); //sposto il puntatore di 2 byte dalla posizione corrente(va alla casella 17)
fread(&d, sizeof(short int), 1, fp);
printf("d: %hd\n", d);
fread(&c4, sizeof(char), 1, fp);
printf("c4: %c\n", c4);
// fseek(fp, 2, 1); //casella 21
fread(&e, sizeof(long int), 1, fp);
printf("e: %ld\n", e);
fread(&s, sizeof(S), 1, fp);
printf("a: %f\n", s.a);
printf("s: %ld\n", s.e);
fclose(fp);
printf("... done\n");
return 0;
}