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

15,658

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 ) ;
Share:
15,658
sepisoad
Author by

sepisoad

My name is "Sepehr Aryani" and I'm A full-stack develper. I've been working professionally as a software engineer since 2010. My major area of interest is designing and developing services and web apps. I am proficient at C++/Go/Python/Javascript(React/Redux)

Updated on June 24, 2022

Comments

  • sepisoad
    sepisoad almost 2 years

    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?

  • Clifford
    Clifford over 13 years
    @SepiDev: He does not need to, it is you asking the question after all; have you tried it? If you are having a problem with it, explain that in the question, with the code you are using. A float will be written as four bytes; so long as you are not exchanging data between systems with different float encoding or byte order, this will work.
  • Thanatos
    Thanatos over 13 years
    This ignores endianness in the serialization of the float. As long as you never move that serialized form (file, in this case) to a machine of a different endianness, it will work. It also assumes the floating point representations are the same (sort of the same as endianness), but I've never seen anything that wasn't IEEE 754
  • old_timer
    old_timer over 13 years
    @Thanatos the questioner wanted to think in terms of something bigger than a byte (on computers that are bit based of course). Certainly endianness was not addressed, and the above is valid as the endianness can be addressed in the ... code not shown. Start with the basics then get into sharing and portability failures after you have some successes.
  • old_timer
    old_timer over 13 years
    @Thantos, look at the texas instruments dsp processors, the family that has been around forever, I think there is a core in the omap ARM processors. They use a different, simpler, floating point format. In some respects far superior to IEEE 754 (and in others inferior of course), certainly faster and far less likely to have bugs in the logic, less power, etc.
  • old_timer
    old_timer over 13 years
    @SepiDev. yep not only have I tried this I have written similar code just about every day for more than 20 years. Many thousands to tens of thousands of freads and fwrites. All of them worked on all the operating systems used.
  • Dan Bechard
    Dan Bechard over 7 years
    @old_timer It may be beneficial to add the fopen call to demonstrate that the file must explicitly be opened in binary mode for this to work as expected.
  • Dan Bechard
    Dan Bechard over 7 years
    @old_timer Also, is there a particular reason you chose to swap the size and count parameters? What is the benefit of reading in a partial floating point number? stackoverflow.com/questions/8589425/how-does-fread-really-wo‌​rk
  • Jonathan Leffler
    Jonathan Leffler about 6 years
    Doesn't this do the conversion to string (from string) that the question asks to avoid doing?