Write double to text file, then read it back later without losing precision

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 19 08:57:52 PDT 2014


Andrew Brown:

> I would like to write a double to a text file as hexadecimal 
> and then read it back in without losing information.

Is this good enough for you?

void main() {
     import std.stdio, std.math;

     auto fout = File("ouput.txt", "w");
     fout.writef("%a", PI);
     fout.close;

     auto fin = File("ouput.txt");
     real r;
     fin.readf("%f", &r);
     fin.close;
     assert(PI == r);
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list