#include <stdio.h>
#include <stdlib.h>
int main(void)
{
#define SIZE 5
double A[SIZE] = {1.,2.,3.,4.,5.};
FILE * fp = fopen("test.bin", "wb");
fwrite(A,sizeof(double),SIZE,fp);
fclose (fp);
double B[SIZE];
fp = fopen("test.bin","rb");
fpos_t pos;
if (fgetpos(fp,&pos) != 0)
{
perror("fgetpos()");
fprintf(stderr,"fgetpos() failed in file %s at line # %d\n", __FILE__,__LINE__-3);
exit(EXIT_FAILURE);
}
int ret_code = fread(B,sizeof(double),1,fp);
printf("%.1f; read count = %d\n", B[0], ret_code);
if (fsetpos(fp,&pos) != 0)
{
if (ferror(fp))
{
perror("fsetpos()");
fprintf(stderr,"fsetpos() failed in file %s at line # %d\n", __FILE__,__LINE__-5);
exit(EXIT_FAILURE);
}
}
ret_code = fread(B,sizeof(double),1,fp);
printf("%.1f; read count = %d\n", B[0], ret_code);
fclose(fp);
return EXIT_SUCCESS;
}