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

P

Pasquale Minervini

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!
 
P

Pasquale Minervini

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!
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top