std::tmpnam(3)
πΈ NAME πΈ
std::tmpnam β std::tmpnam. Welcome to the std::tmpnam(3) page from the C++ Standard Libary, bestie π. This is the function that makes a filename vibe into existence.
π Synopsis
Defined in header <cstdio>:
char* tmpnam( char* filename );
It creates a 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 π.
Also, std::tmpnam modifies static state and is not required to be thread-safe. Not thread-safe, periodt. π
π Parameters
filenameβ pointer to the character array capable of holding at leastL_tmpnambytes, to be used as a result buffer. If a null pointer is passed, a pointer to an internal static buffer is returned.
π Return value
filenameiffilenamewas 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.
π§ Notes
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. That's the race-condition jump scare π.
Bestie, this is the important bit: the standard functionstd::tmpfileand the POSIX functionmkstempdo not have this problem (creating a unique directory using only the standard C library still requires the use oftmpnam). No cap.
POSIX systems additionally define the similarly named function tempnam, which offers the choice of a directory (which defaults to the optionally defined macro P_tmpdir). Extra choices, iconic. β¨
π» Example
// Run this code
#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/fileDjwifs
temporary file name: /tmp/fileEv2bfW
π See also
tmpfileβ creates and opens a temporary, auto-removing file (function).temp_directory_pathβ returns a directory suitable for temporary files (C++17) (function).- C documentation for
tmpnam.
π That's the whole page, bestie! Source: http://cppreference.com Β· version/date: 2024.06.10 Β· manual tag: std::tmpnam(3). Bye! πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms