views:

47

answers:

1

Hi, I am using *open_memstream* in a library of mine, but I would like to port this library to MSVC. It seems there are no equivalent function available, but is there something similar enough?

What *open_memstream* does is it takes a char** destination and size and returns a FILE* which you many write to, the data is stored in a dynamically allocated buffer (accessible from the char** argument). When closing the FILE the char** contains the data that was written to the stream. This makes an easy way to construct large and complex string streams.

While it is possible to both read and seek from the memstream I only write to it.

Is there a way to open a similar memory FILE stream in MSVC? Also, this is pure C, no C++.

+1  A: 

A similar function on Windows would be CreateStreamOnHGlobal(). That however works with the IStream COM interface, it isn't a drop-in replacement for FILE. You might want to take a peek at the Cygwin source code to see what they did.

Hans Passant
Cygwin seems to use newlib runtime library, which implements it's own FILE structure and thus implements fopencookie (and open_memstream). At least that's what I find in the source.
ext
Yes, that's not unlikely. It would have to know that the FILE* references a non-file type resource. If you don't want to use Cygwin then you're kinda doomed to replace your memory stream code.
Hans Passant