C - create a file in memory

I am writing a small program in simple C here. I need to create a file directly inside the memory (not written to the hard drive) Currently I can use fopen("filename.txt,"wb") to write to the file.

I know that in linux you can use fmemopen() . Is there a similar solution for win32?

+15
source share
4 answers

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:

  1. Creation of a temporary file; that is, fickle. In other words, use CreateFile with a path created from GetTempPath and possibly GetTempFileName .

  2. ... with FILE_ATTRIBUTE_TEMPORARY set to FILE_ATTRIBUTE_TEMPORARY [1]; that is, as far as possible, supported by memory.

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

+5
source

Such functionality is not supported inside Windows. However, you can rewrite the higher-level stdio functions to access the memory buffer rather than the file, or simply implement stdio-like functions. Or you can implement a RAM disk , but installing the RAM disk driver itself may not match your "do not write to disk" rule.

+1
source

I would not use fopen since this creates a file.

To create a file in memory, you will need to create a file and save it as you like.

You will need to create an array of some data type. I would use char *, but you could use whatever you want to encode. Then you can read and write the file by adding information to the array.

0
source

You can achieve something close with MapViewOfFile. The Win32 API you should use for this:

CreateFile (...)

CreateFileMapping (...)

MapViewOfFile (...)

Remember to cancel and close the file association as soon as you are done.

0
source

Source: https://habr.com/ru/post/1266933/


All Articles