std::tmpnam
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <cstdio> 中定义
|
||
char* tmpnam( char* filename ); |
||
不命名目前现有的文件创建一个唯一的文件名,并将其存储在字符串所指向
filename
。该功能能够产生高达TMP_MAX唯一的文件名,但他们中的一些或全部可能已经在使用中,从而不适合的返回值. 原文:
Creates an unique filename that does not name a currently existing file, and stores it in the character string pointed to by
filename
. The function is capable of generating up to TMP_MAX of unique filenames, but some or all of them may already be in use, and thus not suitable return values. 目录 |
[编辑] 参数
filename | - | 能够保持至少L_tmpnam字节,被使用作为一个结果缓冲器的字符数组的指针。如果
NULL 传递,返回一个指针,指向一个内部静态缓冲区.原文: pointer to the character array capable of holding at least L_tmpnam bytes, to be used as a result buffer. If NULL is passed, a pointer to an internal static buffer is returned. |
[编辑] 返回值
filename
如果filename
NULL。否则,返回一个指针,指向一个内部静态缓冲区。如果没有合适的文件名,可以产生NULL返回.原文:
filename
if filename
was not NULL. Otherwise a pointer to an internal static buffer is returned. If no suitable filename can be generated, NULL is returned.[编辑] 注释
空指针参数调用时,这个函数修改了一个全局对象。如果另一个线程调用
std::tmpnam
行为是未定义的空指针参数的同时,由于数据争用.原文:
When called with null pointer argument, this function modifies a global object. If another thread calls
std::tmpnam
with null pointer argument at the same time, the behavior is undefined due to a data race.虽然名称所产生的
std::tmpnam
是很难猜测的,它可能是由另一个进程之间的那一刻std::tmpnam
回报的那一刻,这个程序将尝试使用返回的名称创建一个文件,文件的名字。标准功能std::tmpfile和POSIX功能mkstemp没有这个问题原文:
Although the names generated by
std::tmpnam
are difficult to guess, it is possible that a file with that name is created by another process between the moment std::tmpnam
returns and the moment this program attempts to use the returned name to create a file. The standard function std::tmpfile and the POSIX function mkstemp do not have this problem.[编辑] 示例
#include <iostream> #include <cstdio> #include <string> int main() { std::string name1 = std::tmpnam(nullptr); std::cout << "temporary file name: " << name1 << '\n'; char name2[L_tmpnam]; if(std::tmpnam(name2)) std::cout << "temporary file name: " << name2 << '\n'; }
Possible output:
temporary file name: /tmp/fileDjwifs temporary file name: /tmp/fileEv2bfW
[编辑] 另请参阅
创建并打开一个临时的,自动删除文件 原文: creates and opens a temporary, auto-removing file (函数) | |
C documentation for tmpnam
|