Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a C FILE object to read/write in memory

Tags:

c++

file

memory

I am using an API that takes a FILE * and am using that to create a data buffer in memory:

std::shared_ptr<FILE> f(tmpfile(), fclose);
write_to_file(f.get());
rewind(f.get());
auto data = make_file_buffer(f.get());
return data;

This works, but is slower than writing to a memory buffer.

Is it possible to get this to write to a memory file and avoid reading/writing to disk (like stdin/stdout/stderr read/write to the console)?

NOTE: I am using Linux, so have access to Linux and POSIX APIs.

like image 281
reece Avatar asked May 29 '13 13:05

reece


2 Answers

Yes, this is possible, see fmemopen.

like image 188
Damon Avatar answered Oct 19 '22 02:10

Damon


I wrote a comment above, but I'll flesh it out for an answer.

Since your API expects a FILE handle, the easiest way would be to create a file on an in-memory filesystem, such as something mounted with ramfs or tmpfs on Linux.

There's one of these created by default on most Linux systems under /dev/shm, so creating any file under there will still exist as an actual file, but will only exist in memory. Of course, if you reboot, the contents of the file will be lost. I also don't think it can be paged to disk, so try and avoid writing huge files or you'll take up all the space in your memory.

like image 31
slugonamission Avatar answered Oct 19 '22 02:10

slugonamission