std::tmpfile
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <cstdio> 中定义
|
||
FILE* tmpfile(); |
||
创建并打开一个临时文件具有独特的自动生成的文件名.
原文:
Creates and opens a temporary file with unique auto-generated filename.
打开该文件作为二进制文件进行更新(std::fopen访问模式
"wb+"
)。至少TMP_MAX文件可以打开一个程序的生命周期内(此限制可能会与std::tmpnam,并可能进一步限制FOPEN_MAX)原文:
The file is opened as binary file for update (as by std::fopen with access mode
"wb+"
). At least TMP_MAX files may be opened during the lifetime of a program (this limit may be shared with std::tmpnam and may be further limited by FOPEN_MAX)如果程序关闭该文件,例如通过执行std::fclose,该文件被自动删除.
原文:
If the program closes the file, e.g. by executing std::fclose, the file is automatically deleted.
如果程序正常终止(通过调用std::exit,返回main等),通过调用
std::tmpfile
打开的所有文件也会被自动删除.原文:
If the program terminates normally (by calling std::exit, returning from main, etc), all files that were opened by calling
std::tmpfile
are also automatically deleted.如果程序异常终止,这是实现定义的,如果删除这些临时文件.
原文:
If the program terminates abnormally, it is implementation-defined if these temporary files are deleted.
目录 |
[编辑] 参数
(无)
[编辑] 返回值
相关的文件流或NULL,如果发生了错误
原文:
The associated file stream or NULL if an error has occurred
[编辑] 注释
在某些实现中(如Linux),这个功能实际上是创建,打开,并立即删除该文件从文件系统:只要一个打开的文件描述符已删除的文件的程序举行,该文件存在,但由于它是删除,它的名字并没有出现在任何目录中,所以没有其他进程可以打开它。一旦文件描述符是封闭的,该文件所占据的空间是由文件系统“回收.
原文:
On some implementations (e.g. Linux), this function actually creates, opens, and immediately deletes the file from the file system: as long as an open file descriptor to a deleted file is held by a program, the file exists, but since it was deleted, its name does not appear in any directory, so that no other process can open it. Once the file descriptor is closed, the space occupied by the file is reclaimed by the filesystem.
[编辑] 示例
#include <iostream> #include <cstdio> #include <cstdlib> int main() { FILE* tmpf = std::tmpfile(); std::fputs("Hello, world", tmpf); std::rewind(tmpf); char buf[6]; std::fgets(buf, sizeof buf, tmpf); std::cout << buf << '\n'; // Linux-specific method to display the tmpfile name std::system("ls -l /proc/self/fd/3"); }
Possible output:
Hello lrwx------ 1 user group 64 Jun 27 00:28 /proc/self/fd/3 -> /tmp/tmpfXu58Zi (deleted)
[编辑] 另请参阅
返回一个唯一的文件名 (函数) | |
C documentation for tmpfile
|