Storing and retrieving

Hans Aberg haberg at
Sat Jul 2 10:31:44 CEST 2005

At 03:53 +0000 2005/07/02, mrgrynch at wrote:
>I have a need to preserve a bunch of GMP floating point values (from 
>an array of mpf_class objects) for retrieval at a later time.  The 
>experiement I am running will take about a year, and I'd hate to 
>lose it all if the power goes out, etc...
>  I was thinking the easiest way to do this is with strings.  Has 
>anyone done this before?

One can write binary data to a file and read it back, but I gather if 
the core parts that the GMP relies on would somehow change, it would 
make the file binary data unreadable. So the safest way, though 
slower, is to write it out as text, and read it back again. You are 
then embarking, in its extension, on the path of making two programs, 
a parser that can read this kind of data, and an expresser, or 
"pretty-printer", that can write out it. Ultimately, if you develop a 
computer language, you may need the use of tools such as the lexer 
generator Flex and the parser generator Bison.

But start simply, expand at need.

>Reading through the documentation, I'm not entirely sure about the 
>argument list to be passed to the functions which handle this, nor 
>how they would work through the C++ class interface, which I am 
>using.  Would I have to extract a reference to the underlying C 
>structure before converting to and from strings?

GMP has C read and write functions, and further, some C++ support. I 
haven't used the latter, as I wrote my own C++ wrap. I can send it to 
you by private mail, if you so like.

The major drawback of my C++ wrap is that I had my own version of the 
GMP read function. So it means that if GMP changes that, I will have 
to do some rewriting. But this part is there only to make a C++ nice 
istream interface; you could use something more version-stable, if 
you like. Otherwise, it is just a C++ interface on top of C, using 
reference counts to avoid unneeded recopying.
   Hans Aberg

More information about the gmp-discuss mailing list