Question: from an in-memory buffer to a FILE* stream

Discussion in 'C++' started by Pasquale Minervini, Apr 13, 2009.

  1. Hi everyone; it's a few days I'm trying to find a decent (and
    portable) solution to the following problem:

    Imagine you've a function whose prototype is like the following:
    void function(FILE *);
    i.e. it gets data from a FILE* stream, but the data you want to pass
    is stored in an in-memory stream; in your opinion, what's the best
    solution to this problem? (Note: I don't actually have access to
    function's internals)

    The best solution I've found is by using pipe(), i.e. creating two
    piped descriptors (call them fdread and fdwrite), turning them in
    FILE* streams using fdopen(), writing on the fdwrite and passing
    fdread to the function, but it's quite a mess. Any idea?

    Thanks a lot in advance for every comment/suggestion/anythin!
    Pasquale Minervini, Apr 13, 2009
    #1
    1. Advertising

  2. Hi Sam, thanks a lot for your answer!
    I knew about fmemopen() and open_memstream() (they would have been
    ideal), but I didn't use them since they are GNU extensions and could
    have caused portability problems in my code.
    Actually in C++ it's possible to do a similar thing with std::string
    and std::iostream (by using std::stringstream), so I was wondering if
    there's something also for std::string (or char*) and FILE*.
    Thanks a lot again for your answer!

    On 13 Apr, 02:14, Sam <> wrote:
    > Pasquale Minervini writes:
    > > Hi everyone; it's a few days I'm trying to find a decent (and
    > > portable) solution to the following problem:

    >
    > > Imagine you've a function whose prototype is like the following:
    > > void function(FILE *);
    > > i.e. it gets data from a FILE* stream, but the data you want to pass
    > > is stored in an in-memory stream; in your opinion, what's the best
    > > solution to this problem? (Note: I don't actually have access to
    > > function's internals)

    >
    > Although this is really a C, and not a C++ question, systems based on glibc
    > or which are conformant to POSIX-1.2008 have an fmemopen() function that
    > constructs a FILE * which reads/writes from a memory buffer, which is
    > exactly what you want.
    >
    > > The best solution I've found is by using pipe(), i.e. creating two
    > > piped descriptors (call them fdread and fdwrite), turning them in
    > > FILE* streams using fdopen(), writing on the fdwrite and passing
    > > fdread to the function, but it's quite a mess. Any idea?

    >
    > > Thanks a lot in advance for every comment/suggestion/anythin!

    >
    > If your platform does not implement fmemopen(), a somewhat cleaner
    > alternative is to create a temporary file with tmpfile(), dump your buffer
    > there, and pass its FILE pointer to your function.
    >
    >  application_pgp-signature_part
    > < 1KVisualizzaScarica
    Pasquale Minervini, Apr 13, 2009
    #2
    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. Raja
    Replies:
    12
    Views:
    24,350
    John Harrison
    Jun 21, 2004
  2. Replies:
    9
    Views:
    617
    Alex Buell
    Apr 27, 2006
  3. Replies:
    2
    Views:
    590
    sergejusz
    Mar 26, 2007
  4. Steven Woody
    Replies:
    13
    Views:
    6,045
  5. Roedy Green

    byte stream vs char stream buffer

    Roedy Green, May 7, 2014, in forum: Java
    Replies:
    20
    Views:
    167
    Silvio
    May 18, 2014
Loading...

Share This Page