Output stream data to a string

  • Thread starter walter.preuninger
  • Start date
W

walter.preuninger

I am writing a program, using the gmp library. I need to take the
output (mpz_out_str) and put it back into a string. Is there an easy
way to take stream output, and 'place it' into a string?

I can do it with fopen, mpz_out_str, rewind, fgets.

Is there a better way such as using pipe or fdopen, dup etc?

Basically, I want (headers, variable declarations, error checking
excluded)

output=fopen("data.out",'w+");
mpz_out_str(output,10,mpz_data); /* Outputs mpz_data in base 10) */
rewind(output);
fgets(output,buffer,40);

Buffer would contain the human readable value contained in mpz_data.

Thanks,

Walter
 
R

Rod Pemberton

I am writing a program, using the gmp library. I need to take the
output (mpz_out_str) and put it back into a string. Is there an easy
way to take stream output, and 'place it' into a string?

I can do it with fopen, mpz_out_str, rewind, fgets.

Is there a better way such as using pipe or fdopen, dup etc?

Basically, I want (headers, variable declarations, error checking
excluded)

output=fopen("data.out",'w+");
mpz_out_str(output,10,mpz_data); /* Outputs mpz_data in base 10) */
rewind(output);
fgets(output,buffer,40);

Buffer would contain the human readable value contained in mpz_data.

Is there a reason you can't use sprintf()? You didn't say what format
mpz_data is in.

string:
sprintf(buffer, "%s", mpz_data)

unsigned long:
sprintf(buffer, "%08lx", mpz_data)


Rod Pemberton
 
W

walter.preuninger

the call to mpz_out_str is

size_t mpz_out_str (FILE *stream, int base, mpz_t op)

where op is the gmp integer data type (binary)

What I am asking is for a way to implement sprintf for stream output.

Thanks,

Walter
 
R

Rod Pemberton

the call to mpz_out_str is

size_t mpz_out_str (FILE *stream, int base, mpz_t op)

where op is the gmp integer data type (binary)

What I am asking is for a way to implement sprintf for stream output.

Thanks,

Isn't that called fprintf()? Or am I still misunderstanding?

RP
 
W

walter.preuninger

Rod said:
Isn't that called fprintf()? Or am I still misunderstanding?

RP

I think you still are [misunderstanding].

I can call mpz_out_str(stdout, 10, value) where value contains gmp's
representation of an number, (for my example, 31415926535) and on
stdout, i get
31415926535

If I call mpz_out_str(outfile, 10, value) and outfile has been fopen'ed
with the file name "gmp.out") I get
31415926535 in the file gmp.out.

I want to be able to feed the output of mpz_out_str back into a
character array.

If mpz_out_str returned a char *, I could do

sprintf(buffer,"%s",mpz_out_str(stdout, 10, value));

Walter
 
C

Cesar Rabak

(e-mail address removed) escreveu:
Rod Pemberton wrote: [snipped]


I think you still are [misunderstanding].

I can call mpz_out_str(stdout, 10, value) where value contains gmp's
representation of an number, (for my example, 31415926535) and on
stdout, i get
31415926535

If I call mpz_out_str(outfile, 10, value) and outfile has been fopen'ed
with the file name "gmp.out") I get
31415926535 in the file gmp.out.

I want to be able to feed the output of mpz_out_str back into a
character array.

If mpz_out_str returned a char *, I could do

sprintf(buffer,"%s",mpz_out_str(stdout, 10, value));
Walter,

This is technically OT in this NG, however, it seems what you want is to
use the gmp library function gmp_sprintf or even better (see manual why)
gmp_snprintf.

HTH
 
K

Keith Thompson

I am writing a program, using the gmp library. I need to take the
output (mpz_out_str) and put it back into a string. Is there an easy
way to take stream output, and 'place it' into a string?

I can do it with fopen, mpz_out_str, rewind, fgets.
[...]

This is really a question about what functionality is provided by gmp,
not about the C programming language.

Probably gnu.utils.help would be a good place to ask this question.
Or you could spend a few minutes with the gmp documentation. (Note
that converting something to a string isn't really "output", so the
information might not be where you're looking for it.

<OT>mpz_get_str</OT>
 
C

CBFalconer

I guess I should read to documentation more...

Probably. You should also learn to include context to avoid
creating such a meaningless article. See below.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top