| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Types and objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <cstdio> | ||
char* tmpnam(char* filename); | ||
Creates a unique filename that does not name a currently existing file, and stores it in the character string pointed to byfilename. The function is capable of generating up toTMP_MAX of unique filenames, but some or all of them may already be in use, and thus not suitable return values.
std::tmpnam modifies static state and is not required to be thread-safe.
Contents |
| filename | - | pointer to the character array capable of holding at leastL_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 iffilename was not a null pointer. Otherwise a pointer to an internal static buffer is returned. If no suitable filename can be generated, a null pointer is returned.
Although the names generated bystd::tmpnam are difficult to guess, it is possible that a file with that name is created by another process between the momentstd::tmpnam returns and the moment this program attempts to use the returned name to create a file. The standard functionstd::tmpfile and the POSIX functionmkstemp do not have this problem (creating a unique directory using only the standard C library still requires the use oftmpnam).
POSIX systems additionally define the similarly named functiontempnam, which offers the choice of a directory (which defaults to the optionally defined macroP_tmpdir).
#include <cstdio>#include <iostream>#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/fileDjwifstemporary file name: /tmp/fileEv2bfW
| creates and opens a temporary, auto-removing file (function)[edit] | |
(C++17) | returns a directory suitable for temporary files (function)[edit] |
C documentation fortmpnam | |