Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read and write float numbers from and into binary files? [duplicate]

How should a program read and write float numbers from and into binary files in C or Vala language?

The common APIs to do writing and reading are generally designed to write in byte format. I mean you have to write arrays of one-byte data into file and read in the same format.

I'm looking for a way to write and read in float format. without typecasting and without having to change the number into string. Is it possible?

like image 847
sepisoad Avatar asked Dec 16 '10 20:12

sepisoad


1 Answers

fwrite() and fread() or write() and read() will work just fine.

float da, db ;
    ...
fwrite( &da, 1, sizeof(da), fpout ) ;
    ...
fread( &db, 1, sizeof(db), fpin ) ;
like image 156
old_timer Avatar answered Sep 30 '22 12:09

old_timer