I m trying to read 4-byte numbers stored in a binary file, but I don t seem to be getting the correct output. Here s my code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int res;
FILE *file;
float v;
//Open file
file = fopen("prj3.dat", "rb");
if (!file)
{
printf("Unable to open file input.dat
");
}
else
{
res = fread(&v, sizeof(v), 1, file);
if(res==-1)
{
printf("Unable to open file input.dat
");
}
else
{
printf("v = %f
", v);
}
}
return;
}
My output is v = -961576900.0000000, but it should be v = 3.14159. Any ideas as to where my problem is?
NB. input.dat is the binary file: 11010000 00001111 01001001 01000000
Thanks