reading binary file content into an mpz_t variable

Torbjorn Granlund tg at
Mon Jan 17 08:48:39 CET 2011

Erick Paquin <info at> writes:

  I would basically like to read the "entire" content of a file (its
  hex/binary content) into an mpz_t variable for later processing.
  I've tried with both the mpz_inp_str and the mpz_inp_raw functions and when
  I try to see the number of bytes read from these functions it always either
  returns 0 (as if an error occurred) or it crashes the application with a
  memory segmentation fault.
Then you've done something wrong, I am afraid.

  Actually I am not also 100% sure of how to "read" the size_t returned value
  from the gmp functions (do you assign the function to an int variable and
  read it?). Any help on that too would be awesome.
I don't understand what you don't understand.  Aren't the return values
unambiguously documented in the GMP manial?

  How would you go about in the simplest way possible read the entire content
  of a file into an mpz_t variable?

I\d you mpz_inp_str, mpz_inp_raw, or perhaps stdio followed by a
mpz_import, depemnding on the file format.

  I've also tried with fread which actually works but then to transfer the
  content of my fread into an mpz_t variable I couldn't find how.
There are functions for that, mentioned in the manual.


More information about the gmp-discuss mailing list