Reading a file in as a number.

delta trinity deltatrinity at hotmail.com
Thu Dec 11 20:10:37 CET 2003


You can read the file into a buffer, and then use mpz_import.

For example, on a x86 system, to load a 20 'unsigned long' (4-bytes or 1 
DoubleWORD) array:

{
   unsigned long Buffer[20];
   mpz_t Value;

   //... (Read 20 unsigned long from file, into Buffer)

   mpz_import(Value,20,-1,sizeof(Buffer[0]), -1, 0, Buffer);
   //param 1: The mpz_t struct to fill
   //Param 2: The number of 'words' (20 here).  Note that 'words' here is 
not to be confused with the x86 standard denomination of a 16-bit unsigned 
value (WORD).  This can by any arbitary size, which get converted down to 
limbs by GMP.
   //Param 3: Order (-1 for lowest word first)
   //Param 4: Word size in bytes (size of a 'word'.  In this case, the size 
in byte of a single element of the buffer).
   //Param 5: Endian.  On x86, the first byte of each words, that is, the 
byte at the lowest address, is the least significant byte. (-1 for least 
significant byte first)
   //Param 6: Nails.  On some systems, some arithmetics/logic operations can 
only be done in the lower bits of that system word.  For example, where on 
such system, a word size would be 32 bits, some operations can only be done 
on the lowest 16 bits.  So, only 2 bytes out of 4 are used by GMP for 
achieving faster operations, the other are unused.  On x86 architecture, 
this limitation does not apply as the processor ALU can operate on all bits 
of a word.  (0 meen use all bits).

   //...
}


Eric

>From: Ernst Berg <ernst_berg at sbcglobal.net>
>To: gmp-discuss at swox.com
>Subject: Reading a file in as a number.
>Date: Thu, 11 Dec 2003 02:19:09 -0800 (PST)
>
>
>  Hello
>
>  C programming is my hobby and I must admit I am still
>learning.
>  I'm the kind of programmer that doesn't look too deep
>into how a function works and often I use examples to
>aid me in writing programs.
>   I need to understand how I would load a file into a
>number in gmp.  I figure I would, maybe, use the
>Function: size_t mpz_inp_str (mpz_t rop, FILE *stream,
>int base).
>
>  My request is to see an example of how to read in a
>binary file into a gmp variable such as type mpz_t.
>
>  If you can share an example of reading a plain binary
>file into a variable such as mpz_t I thank you.
>
>  If there is a web site or a FAQ I should read please
>tell me as well.
>
>My Email is ernst_berg at sbcglobal.net
>
>
>
>_______________________________________________
>gmp-discuss mailing list
>gmp-discuss at swox.com
>https://gmplib.org/mailman/listinfo/gmp-discuss

_________________________________________________________________
Shop online for kids’ toys by age group, price range, and toy category at 
MSN Shopping. No waiting for a clerk to help you! http://shopping.msn.com



More information about the gmp-discuss mailing list