fdopen is located in POSIX.1-1990, and Windows supports it in the form of _fdopen . Using it with _get_osfhandle and _open_osfhandle , it allows you to create the proper FILE * from a Windows HANDLE file that works with the rest of the C standard library. See also the question. Is there a Windows similar to fdopen for HANDLE? for more information about this.
Now the rest of the problem is creating a Windows file with memory backup. It turns out there are different approaches to this that bring it closer under NT. The most common is:
Creation of a temporary file; that is, fickle. In other words, use CreateFile with a path created from GetTempPath and possibly GetTempFileName .
... with FILE_ATTRIBUTE_TEMPORARY set to FILE_ATTRIBUTE_TEMPORARY [1]; that is, as far as possible, supported by memory.
... and with FILE_FLAG_DELETE_ON_CLOSE set [2]; that is, a hint that it will never need to be written if it does not fit in memory.
As long as there is enough memory to store the file (i.e. low enough memory pressure), the disk will not be used. Check out this MSDN post from Larry Osterman that describes this approach. Funny, he calls such files "temporary" temporary files.
Note that this is not entirely equivalent to fmemopen , which will fail if the file does not fit in a predefined memory area [3] (i.e. fmemopen installs fmemopen with clean memory). However, for many purposes this is fairly close (and sometimes might require potential disk support).
In addition, see How to prevent a memory card from being opened in a temporary delete-on-close Windows file from being flushed to disk, some discussion of these file attributes and other possible approaches is discussed.
Finally, for a complete example that combines both parts (i.e. creates a file with memory support and then wraps it in a FILE * descriptor), take a look at the implementations from the fmem library (which was written to provide cross-platform). solution to this problem) or one from the libconfuse library .
[1]
Specifying the FILE_ATTRIBUTE_TEMPORARY attribute causes file systems to avoid writing data back to FILE_ATTRIBUTE_TEMPORARY storage if enough cache is available, because the application deletes the temporary file after the handle is closed. In this case, the system can completely avoid writing data. Although it does not directly control data caching in the same way as the previously mentioned flags, the FILE_ATTRIBUTE_TEMPORARY attribute tells the system to store as much as possible in the system cache without writing and, therefore, may be of interest to certain applications.
[2]
The file should be deleted immediately after closing all of its descriptors, which includes the specified descriptor and any other open or duplicated descriptors.
[3]
Attempts to write more size bytes to the buffer result in an error.