Thursday 15 September 2011

How to read a binary file and display the output as a float in C? -


I am trying to read a 4-byte number stored in the binary file, but I do not think the correct output here is my code Is:

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; Int main () {int res; File * file; Float V; // open file file = FOPAN ("pr3 DAT", "RB"); If (! File) {printf ("Unable to open file input." \ N "); } And {res = fread (& amp; v, size (v), 1, file); If (res == - 1) {printf ("Unable to open file input.dat \ n"); } Other {printf ("v =% f \ n", v); }} Return; }  

My output is v = -961576900.0000000, but it should be v = 3.14159 wherever my problem is?

NB Input.dat is a binary file: 11010000 00001111 01001001 01000000

thanks

Make sure the fread sizeof (float) has the same value, which is the size you are reading. Also make sure your file cursor is in the right place, otherwise use fseek Finally, did your file be written in a large Andian machine? Was it saved correctly? Code seems fine for me.


No comments:

Post a Comment