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

发布于 2024-09-03 17:00:42 字数 106 浏览 12 评论 0原文

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

是否可以?

这样的事该怎么办呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

删除→记忆 2024-09-10 17:00:42

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

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

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

好久不见√ 2024-09-10 17:00:42

如果您的操作系统支持(Unixoid 系统和 Windows),您可以尝试使用 内存映射文件

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

久伴你 2024-09-10 17:00:42

您可以在 C 中使用 popen() 函数来完成此操作:

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

如果您的外部程序从 stdin 读取其输入,则这是可能的。

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.

还给你自由 2024-09-10 17:00:42

你可以使用管道()

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

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.)
魂ガ小子 2024-09-10 17:00:42

是的,这是可能的。您可以通过进程间通信机制将数据传输到其他应用程序:

  1. 取决于您的操作系统,你在这里有不同的选择。您可以创建一个管道,正如其他海报在这里提到的那样,因为许多操作系统都有管道。
  2. 您还可以使用共享内存。
  3. 您可以简单地将其写入文件,然后在其他应用程序中打开该文件。
  4. 许多操作系统还有其他可以使用的技术。

编辑:MSDN 列出了适用于 Windows 此处的所有 IPC 机制

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文