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

Hi all,
                في س، 18-12-2010 عند 14:47 +0100 ، كتب Aleksander Wabik:
Phil Housley <undeconstructed gmail com> writes:

Aside: The following doesn't work:

public void main() {
   float flt = 9.12f;
   var output ="./data.txt","w");
   output.printf("%f", flt);
   float in_flt = 5f;
   var input ="./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.

This breaks experimental non-null checking; keeping in mind the word
'experimental' I may suppose, that some day this will not be
experimental any more and will become part of language. (BTW. using
nullable variables is now extremely inconvenient in non-null mode, so
I'd not advise to make 'output' nullable).
Well, you could also declare output as a pointer and explicitly delete
it (but pointer are also inconvenient, as they need -> instead of .),
but the easiest way (and, I believe, the way it's supposed to be used)
is to put it in a separate block, so it goes out-of-scope as soon as
you're finished with it. e.g.

  var output = new FileStream (...);
  // do something with the file
// here, as we no longer need the file, it's closed


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