Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of open_memstream for MSVC

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++.

like image 811
ext Avatar asked May 19 '10 09:05

ext


2 Answers

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.

like image 66
Hans Passant Avatar answered Sep 29 '22 20:09

Hans Passant


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()

like image 20
maxirmx Avatar answered Sep 29 '22 19:09

maxirmx