I have to work with two C programs that communicate via a file-based interface. That is, each of them has a main loop where it polls three or four files (fopen, fscanf), reacts to what it reads and eventually makes its own changes to the files (fprintf) for the other process to read.
Now I have to condense these two programs into a single program, with minimal changes to the program logic and the code in general. However, mainly for aesthetic reasons I’m supposed to replace the file-based communication with something in-memory.
I can imagine a few hacky ways to accomplish this, but I’m sure that stackoverflow will give me a hint at a beautiful solution 🙂
Since you tagged this Linux, I’m going to suggest
open_memstream. It was added to POSIX with POSIX 2008, but it’s been available on glibc-based Linux systems for a long time. Basically it lets you open aFILE *that’s actually a dynamically-growing buffer in memory, so you wouldn’t have to change much code. This “file” is write-only, but you could simply usesscanfinstead offscanfon the buffer to read it, or usefmemopen(which doesn’t have the dynamic-growth semantics but which is very convenient for reading from in-memory buffers).