如何将RAM数据视为真实文件?

时间:2010-05-30 22:59:33

标签: c++ c windows share ram

所以我的程序中有一些临时数据(在RAM中)。我想以某种方式使它看起来像是一个文件(例如将其发送到另一个以文件链接作为参数的程序)?

有可能吗?

怎么做这样的事情?

5 个答案:

答案 0 :(得分:15)

为什么不简单地将文件写入磁盘?如果写入磁盘太慢,可以将FILE_ATTRIBUTE_TEMPORARY标志传递给CreateFile以将数据保存在缓存中(并避免将其写入物理设备)。

有时候明显的解决方案是最好的......

答案 1 :(得分:11)

如果您的操作系统(Unixoid系统和Windows支持)支持,您可以尝试使用memory-mapped files

答案 2 :(得分:1)

您可以使用popen()功能在C中执行此操作:

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

如果您的外部程序从stdin读取输入,则可以这样做。

答案 3 :(得分:0)

你可以使用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.)

答案 4 :(得分:0)

是的,有可能。您可以通过interprocess communication机制将数据传输到其他应用程序:

  1. 根据您的操作系统,您可以在此处使用不同的选项。您可以创建一个管道,正如其他海报在这里提到的那样,因为许多操作系统都有管道。
  2. 您也可以使用共享内存。
  3. 您只需将其写入文件,然后在其他应用程序中打开该文件即可。
  4. 许多操作系统都有其他可以使用的技术。

  5. 编辑:MSDN列出了可用于Windows here的所有IPC机制。

相关问题