7

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
sepisoad
  • 2,201
  • 5
  • 26
  • 37

2 Answers2

5

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 ) ;
Clifford
  • 88,407
  • 13
  • 85
  • 165
old_timer
  • 69,149
  • 8
  • 89
  • 168
  • 5
    @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. – Clifford Dec 16 '10 at 23:06
  • 2
    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 – Thanatos Dec 16 '10 at 23:16
  • @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 Dec 17 '10 at 05:03
  • @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 Dec 17 '10 at 05:07
  • @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. – old_timer Dec 17 '10 at 05:11
  • @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 Jan 16 '17 at 13:47
  • 1
    @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? http://stackoverflow.com/questions/8589425/how-does-fread-really-work – Dan Bechard Jan 16 '17 at 14:05
-1

In Vala you can do:

public void main() {
    float foutvalue = 5.55;
    { //Need to make vala close the output file!
        var output = FileStream.open("floatfile","w");
        output.printf("%f", foutvalue);
    }

    float finvalue = 0.0;
    {
        var input = FileStream.open("floatfile", "r");
        input.scanf("%f", out finvalue);
    }
    print(@"$finvalue\n");
}
Treviño
  • 2,999
  • 3
  • 28
  • 23