gpt4 book ai didi

c - 在 C 中使用 tmpfile() 时的临时文件位置

转载 作者:IT王子 更新时间:2023-10-29 01:08:31 25 4
gpt4 key购买 nike

$ man tmpfile

The standard does not specify the directory that tmpfile() will use. Glibc will try the path prefix P_tmpdir defined in <stdio.h>, and if that fails the directory /tmp.

我正在使用 Ubuntu 13.10 x86_64、gcc 和 libc BTW。

因此,当我尝试使用 tmpfile() 创建临时文件时,我在/tmp 中看不到任何临时文件。 (我可以在 stdio.h 中看到 # define P_tmpdir "/tmp")。这是我使用的代码片段:

#include <stdio.h>

int main(int argc, char **argv)
{
FILE *tmp;

tmp = tmpfile(); // Where's this file?
scanf("%*d");

return 0;
}

$ ./tmpfile
现在,当 scanf 正在等待下一个(冗余)输入时,我应该能够在/tmp 中看到一个临时文件。但我不能。那么这个 tmpfile 到底是在哪里创建的呢?

最佳答案

可能直接删除了目录中的文件条目。在 POSIX 系统上,文件本身在删除后仍然有效,只要您有一个打开的文件描述符即可。 (在您的情况下,隐藏在 FILE* 返回值中。)

使用这种技术,没有人可以潜入并打开该文件,它只能通过您的变量 tmp 访问。

关于c - 在 C 中使用 tmpfile() 时的临时文件位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28608120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com