Re: [Vala] How to write float and double values into a binary file?



Phil Housley <undeconstructed gmail com> writes:

Aside: The following doesn't work:

public void main() {
      float flt = 9.12f;
      var output = FileStream.open("./data.txt","w");
      output.printf("%f", flt);
      float in_flt = 5f;
      var input = FileStream.open("./data.txt", "r");
      int read = input.scanf("%f", out in_flt);
      message("%d %f", read, in_flt);
}

Because in the generated C, the output file isn't closed before
attempting to read. This is a sort-of-bug, as there is no way to
explicitly close a FileStream...

There is a way to close the output file

    output = null;

as fclose is declared a free_function for FileStream.


Well, 'FileStream.write' api looks like below:

         public size_t <http://valadoc.org/glib-2.0/size_t.html> *write* (
uint8 <http://valadoc.org/glib-2.0/uint8.html>[] buf,
size_t<http://valadoc.org/glib-2.0/size_t.html> size
= 1)

I have to cast my floating point number into integer which in my case it is
useless...
In fact it seems impossible to me, because i failed to do that, Since write
function
needs an array of one-byte-values and I have a float value!!!

If I use 'FileStream.printf' function, it writes data into file in ascii
 format.
this is my code sample:

public void main(){
float flt = 9.12f;
var output = FileStream.open("./data.bin","w+b");
output.printf("%f", flt);
 float? in_flt = null;
       var input = FileStream.open("./data.bin", "r+b");
input.scanf("%f", in_flt); // !!!error
}

I need a way write in floating point format and read in the same format,
without casting...
What should i do?

_______________________________________________
vala-list mailing list
vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]