views:

228

answers:

5

So I have some temp data in my program (in RAM). I want to somehow make it seem as it is a file (for example for sending it into another program which takes a file link as argument)?

Is it possible?

How to do such thing?

A: 

You could use pipe()

The pipe() function shall create a pipe and place two file descriptors,
one each into the arguments fildes[0] and fildes[1], that refer to  the
open  file  descriptions for the read and write ends of the pipe. Their
integer values shall be the two lowest available at  the  time  of  the
pipe() call. The O_NONBLOCK and FD_CLOEXEC flags shall be clear on both
file descriptors. (The fcntl() function can be used to set  both  these
flags.)
hhafez
Not a Windows function (see comment to question)
MSalters
yeah I realised that but before the question was edited no OS was specified
hhafez
Although I agree it is not the windows way of doing it is still possible to use POSIX functions like pipe on windows (for example using Cygwin) but I agree this is not the answer I'd give for windows
hhafez
+1  A: 

You can do it in C using the popen() function:

FILE *f = popen("program args", "w");
// write your output to f here using stdio
pclose(f);

This is possible if your external program reads its input from stdin.

Greg Hewgill
popen is not C, it is POSIX.
Billy ONeal
@Billy ONeal: That's true, but any general purpose implementation of C offers such a `popen()` function (though it might be called `_popen()` as in MSVC).
Greg Hewgill
This also assumes the OP wishes to execute/start the external program from inside the one holding the data. Nevertheless, this solution is slick.
sheepsimulator
@Greg Hewgill: Unless you're on Windows. `_popen` does not work correctly unless you start the given application using the Console subsystem rather than the Windows subsystem.
Billy ONeal
Besides, you couldn't pass the pipe name to another Windows application expecting a file name.
MSalters
A: 

Yes, it is possible. You can transfer your data to your other application via an interprocess communication mechanism:

  1. Depending on your OS, you have different options here. You could create a pipe, as other posters have mentioned here, as many OSes have pipes.
  2. You could also use shared memory.
  3. You could simply write it out to a file, and then open up that file in your other application.
  4. Many OSes have other techniques you can use.

EDIT: MSDN lists all the IPC mechanisms available for Windows here.

sheepsimulator
What processes are intercommunicating? Answer doesn't make sense.
samoz
@samoz - The OP describes two programs - his one that has the data in RAM that he wants to send, and another that needs to receive that data. Program == process when they are running in the OS.
sheepsimulator
+10  A: 

If supported by your operating system (Unixoid systems and Windows do), you could try to use memory-mapped files.

Daniel Brückner
+1. This way the data isn't copied around unless the OS decides it must be.
MSalters
+13  A: 

Why not simply write the file to disk? If writing to disk is too slow, you can pass the FILE_ATTRIBUTE_TEMPORARY flag to CreateFile to keep the data in cache (and avoid writing it to the physical device).

Sometimes the obvious solutions are the best...

Dean Harding
This is the proper way to do this. +5 if possible.
samoz
+1 But I guess its a question of how large the cache and the file are, and hoping the first is larger than the second!
mdma
@mdma: true, but if the cache is too small to fit the file, then you could argue that it's probably not good to keep the whole file in memory in the first place. The good thing about `FILE_ATTRIBUTE_TEMPORARY` is that if there's not enough space in the cache, it'll still use the file system so you don't have to worry.
Dean Harding
Why not: because you copy the data from _your_ RAM to _system_ RAM, using double the amount of memory. Memory mapping a file doesn't.
MSalters