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 t开发者_JS百科akes 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++.
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.
https://github.com/Snaipe/fmem is a wrapper for different platform/version specific equivalents of open_memstream
It tries in sequence the following implementations:
- open_memstream.
- fopencookie, with growing dynamic buffer.
- funopen, with growing dynamic buffer.
- WinAPI temporary memory-backed file.
When no other mean is available, fmem falls back to tmpfile()
精彩评论