Bill Allombert on Fri, 04 Nov 2016 15:55:42 +0100


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

Re: Windows gp64 writebin creates unreadable file for \p174


On Tue, Nov 01, 2016 at 02:38:27PM +0000, Jacques Gélinas wrote:
> 1. On my laptop running Windows 8.1-64 and Pari 2.7-64 or 2.8-64,
> binary files become unreadable if reals use 10 words or more.
> 
>                     GP/PARI CALCULATOR Version 2.8.1 (beta)
>           amd64 running mingw (x86-64/GMP-6.0.0 kernel) 64-bit version
> 
> \\---------------------------------------start copy
> 
> default(secure,0)
> 
> \p173
> \\   realprecision = 173 significant digits
> #1.
> \\ 9
> writebin("173-lg2",log(2));log(2)==read("173-lg2")
> \\ 1
> 
> \p174
> \\   realprecision = 192 significant digits (174 digits displayed)
> #1.
> \\ 10
> writebin("174-7",1/7.);1/7.==read("174-7")
> \\ 1
> writebin("174-lg2",log(2));log(2)==read("174-lg2")
> \\  ***   at top-level: ...174-lg2",log(2));log(2)==read("174-lg2")
> \\  ***                                             ^---------------
> \\  *** read: error opening input file [fread]: `FILE*'.

The issue is that fread is doing some CR/LF conversion.
We should use fopen(,"rb"). 
The attached patch seems to fix this bug.
Maybe there is a better place to set the b flag.

Thanks for reporting this!
Bill.
diff --git a/src/language/es.c b/src/language/es.c
index 0a28371..e399b6d 100644
--- a/src/language/es.c
+++ b/src/language/es.c
@@ -3990,7 +3990,7 @@ pari_fopengz(const char *s)
 static FILE*
 try_open(char *s)
 {
-  if (!pari_is_dir(s)) return fopen(s, "r");
+  if (!pari_is_dir(s)) return fopen(s, "rb");
   pari_warn(warner,"skipping directory %s",s);
   return NULL;
 }
@@ -4411,7 +4411,7 @@ file_is_binary(FILE *f)
 void
 writebin(const char *name, GEN x)
 {
-  FILE *f = fopen(name,"r");
+  FILE *f = fopen(name,"rb");
   pari_sp av = avma;
   GEN V;
   int already = f? 1: 0;
@@ -4421,7 +4421,7 @@ writebin(const char *name, GEN x)
     fclose(f);
     if (!ok) pari_err_FILE("binary output file",name);
   }
-  f = fopen(name,"a");
+  f = fopen(name,"ab");
   if (!f) pari_err_FILE("binary output file",name);
   if (!already) write_magic(f);