mpz_out_raw is not writing data to the file stream

Marc Glisse marc.glisse at inria.fr
Fri Jul 19 16:00:11 UTC 2019


On Fri, 19 Jul 2019, Thomas Lowry wrote:

> #include <gmp.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <errno.h>
> int main(int argc, char** argv) {
>  mpz_t b;
>  mpz_init(b);
>
>  //not sure why but without doing something before
>  //the file IO the file write will segfault
>  int len = 5;
>  mpz_t l[5];
>  for(int i = 0; i < len; ++i) {
>    mpz_init(l[i]);
>  }
>  for(int i = 0; i < len; ++i) {
>    mpz_set_ui(l[i], 1);
>  }
>  for(int i = 0; i < len; ++i) {
>    mpz_clear(l[i]);
>  }
>
>  FILE *file = fopen("numbers.dat", "wb");
>  if(file == NULL) {
>    perror("Could not open numbers.dat for writing");
>    printf ("Error no is : %d\n", errno);
>    exit(EXIT_FAILURE);
>  }
>  for(int i = 1; i < 4; ++i) {
>    printf("writing ");
>    mpz_set_ui(b, i);
>    //mpz_pow_ui(b, b, 1<<5);
>    mpz_out_str(stdout, 16, b);
>    printf(" to file\n");
>    mpz_out_raw(b, file);
>  }
>  fclose(file);
>  printf("write finished\n");
>  file = fopen("numbers.dat", "rb");
>  for(int i = 1; i < 4; ++i) {
>    printf("reading ");
>    mpz_inp_raw(b, file);
>    mpz_out_str(stdout, 16, b);
>    printf(" from file\n");
>  }
>  fclose(file);
>  mpz_clear(b);
>  return 0;
> }
>
> I compile this with
> gcc test.c -lgmp

Try adding -Wsystem-headers

(then swap the order of the includes, then look at the new warning)

-- 
Marc Glisse


More information about the gmp-bugs mailing list