std::tmpnam
Defined in header <cstdio>  |   |   | 
|---|---|---|
char* tmpnam( char* filename );  |   |   | 
创建一个不命名当前存在文件的唯一文件名,并将其存储在filename.该函数能够产生最多可达TMP_MAX的唯一文件名,但其中一些或全部可能已经在使用中,因此不适合返回值。
std::tmpnam修改静态,不需要线程安全.
参数
filename  | -  | pointer to the character array capable of holding at least L_tmpnam bytes, to be used as a result buffer. If a null pointer is passed, a pointer to an internal static buffer is returned.  | 
|---|
返回值
filename如果filename不是NULL否则,返回指向内部静态缓冲区的指针。如果无法生成合适的文件名,NULL会被归还。
注记
虽然由std::tmpnam很难猜测,具有该名称的文件可能是由另一个进程在此期间创建的。std::tmpnam返回,当此程序尝试使用返回的名称创建文件时。标准函数std::tmpfile和POSIX函数姆克斯蒂%28使用标准C库创建唯一目录仍然需要使用tmpnam29%。
例
二次
#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';
    }
}二次
可能的产出:
二次
temporary file name: /tmp/fileDjwifs
temporary file name: /tmp/fileEv2bfW二次
另见
tmpfile  | creates and opens a temporary, auto-removing file (function)  | 
|---|---|
temp_directory_path (C++17)  | returns a directory suitable for temporary files (function)  | 
C.Tmpnam文件
 © cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

