Bill Allombert on Thu, 26 Dec 2019 12:49:45 +0100


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

Re: How to write and read GEN to file in PARI?


On Thu, Dec 26, 2019 at 11:59:48AM +0100, E Poen wrote:
> Hi all,
> 
> I'm using PARI in C, and I have bunch of GEN objects where some are
> integers generated using randomi, and some are Qfb elements. Now, the point
> is that I want to write a few of these GEN objects to a file, and then
> afterwards I want to read them from a file. I want to use a single file to
> hold multiple GEN objects. What's the proper way to handle this in PARI?

The same as in GP!

You have two choices:
1) the binary format:
Use writebin() to write, gp_read_file() to read (read() under GP).
If you have several objects, you can use put them in a t_VEC.
If you call writebin() several time on the same file, you will
get a t_VEC with all the object.

2) the text format:
Use write0() to write, gp_read_file() to read (write() under GP).
If you call write0() several time on the same file, you can
use gp_readvec_file() to read them all as a t_VEC.

> I can basically convert them to string using GENtostr and write the strings
> to a file, however, later I do now know how can I read them into GEN as
> some are integers some Qfb, and I see that there is strtoi to read integer,
> but I don't know whether there exists equivalent one for Qfb. Was wondering
> how can one write and read arbitrary PARI objects to a file in C.

To convert to strings, you can use GENtostr to convert and
gp_read_str to read back.

Cheers,
Bill.