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



http://library.gnome.org/devel/glib/stable/glib-GVariant.html <http://library.gnome.org/devel/glib/stable/glib-GVariant.html#g-variant-builder-add-value>
http://www.valadoc.org/glib-2.0/GLib.Variant.html
Might be of some use.

Jonathan Ryan


On 12/17/2010 05:10 PM, Phil Housley wrote:
On 14 December 2010 19:27, Sepehr Aryani<sepehr aryani gmail com>  wrote:
I'm wondering how to write a float or double value into a binary file.
This is one of those things that is actually a lot harder than it
sounds. One issue is that there isn't a universal standard for
representing floats in binary, so instead you just have to pick a
format, and remember to use the same one when you read. In a C app,
that normally means just using the system's format, and hoping to read
back on a similar system.

In order to achieve that, you just cast the float to a void* (casting
in C doesn't actually change any data, just affects compilation,) and
write the correct number of bytes from that pointer. The write()
method in FileStream is really just stdio's fwrite, and will let you
do that. Except that unfortunately it doesn't really map well into
Vala, which wants to view the void* as an array of bytes, and so the
casting thing doesn't work out well.

The best answer I can give is to find/make some sort of serialisation
routine/library. Possibly there is something already built on GIO's
richer stream classes. Either that or use text as a defacto
serialisation format, and do the printf/scanf thing. Someone else
might be able to suggest a more direct solution...

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...

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]