#include <stdio.h>
#include <stdlib.h>
void check(_Bool condition, const char* func, int line)
{
if (condition)
return;
perror(func);
fprintf(stderr, "%s failed in file %s at line # %d\n", func, __FILE__, line - 1);
exit(EXIT_FAILURE);
}
int main(void)
{
#define SIZE 5
double A[SIZE] = {1.1,2.,3.,4.,5.};
const char* fname = "/tmp/test.bin";
FILE* file = fopen(fname, "wb");
check(file != NULL, "fopen()", __LINE__);
const int write_count = fwrite(A, sizeof(double), SIZE, file);
check(write_count == SIZE, "fwrite()", __LINE__);
fclose(file);
double B[SIZE];
file = fopen(fname, "rb");
check(file != NULL, "fopen()", __LINE__);
long int pos = ftell(file);
check(pos != -1L, "ftell()", __LINE__);
printf("pos: %ld\n", pos);
const int read_count = fread(B, sizeof(double), 1, file);
check(read_count == 1, "fread()", __LINE__);
pos = ftell(file);
check(pos != -1L, "ftell()", __LINE__);
printf("pos: %ld\n", pos);
printf("B[0]: %.1f\n", B[0]);
return EXIT_SUCCESS;
}