#include <stdio.h>
#include <assert.h>
#define SIZE 5
int main(void)
{
FILE* fp = fopen("data.bin", "wb");
assert(fp);
size_t rv = fwrite((double[SIZE]) { 3.14, 1.24, 4.38, 5.63, 7.89 }, sizeof(double), SIZE, fp);
assert(rv == SIZE);
fclose(fp);
fp = fopen("data.bin", "rb");
fpos_t pos;
fgetpos(fp, &pos); // store start of file in pos
double d;
rv = fread(&d, sizeof d, 1, fp); // read the first double value from file
assert(rv == 1);
printf("First value in the file: %.2f\n", d);
fsetpos(fp, &pos); // move file position back to the start of the file
rv = fread(&d, sizeof d, 1, fp); // read the first double from file
assert(rv == 1);
printf("First value in the file: %.2f\n", d);
fclose(fp);
}
/*
First value in the file: 3.14
First value in the file: 3.14
*/