Output stream data to a string

Discussion in 'C Programming' started by walter.preuninger@gmail.com, May 1, 2006.

  1. Guest

    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
     
    , May 1, 2006
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > 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
     
    Rod Pemberton, May 2, 2006
    #2
    1. Advertising

  3. Guest

    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
     
    , May 2, 2006
    #3
  4. <> wrote in message
    news:...
    > 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
     
    Rod Pemberton, May 2, 2006
    #4
  5. Guest

    Rod Pemberton wrote:
    > <> wrote in message
    > news:...
    > > 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


    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
     
    , May 2, 2006
    #5
  6. Cesar Rabak Guest

    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

    --
    Cesar Rabak
     
    Cesar Rabak, May 2, 2006
    #6
  7. Guest

    I guess I should read to documentation more...

    Thanks,

    Walter
     
    , May 2, 2006
    #7
  8. writes:
    > 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>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 2, 2006
    #8
  9. CBFalconer Guest

    wrote:
    >
    > 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/>
     
    CBFalconer, May 2, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasmusson, Lars
    Replies:
    1
    Views:
    760
    popov
    Apr 30, 2004
  2. Replies:
    9
    Views:
    640
    Alex Buell
    Apr 27, 2006
  3. Alexander Korsunsky

    get stream mode flags from an opened stream

    Alexander Korsunsky, Feb 17, 2007, in forum: C++
    Replies:
    1
    Views:
    456
    John Harrison
    Feb 17, 2007
  4. dolphin
    Replies:
    6
    Views:
    558
    Thomas Fritsch
    Mar 18, 2007
  5. Kashif Ur Rehman
    Replies:
    2
    Views:
    867
    Tom Hawtin
    May 17, 2007
Loading...

Share This Page