C++ std::tmpnam()

C++ std::tmpnam()


The tmpnam() function generates a unique filename that can be used to create a temporary file without overwriting any existing one i.e. it returns a string containing a file name different from the name of any existing file, and thus suitable to safely create a temporary file without risking to overwrite an existing file.

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.

std::tmpnam modifies static state and is not required to be thread-safe.


If str is a null pointer, the resulting string is stored in an internal static array that can be accessed by the return value. The content of this string is preserved at least until a subsequent call to this same function, which may overwrite it.


If str is not a null pointer, it shall point to an array of at least L_tmpnam characters that will be filled with the proposed temporary file name.


The file name returned by this function can be used to create a regular file using fopen to be used as a temporary file. The file created this way, unlike those created with tmpfile is not automatically deleted when closed. A program shall call remove to delete this file once closed.




char* tmpnam (char* filename);

The tmpnam() function takes a single argument which is a character string and returns a unique filename. This function is capable of generating up to TMP_MAX unique filenames.

It is defined in the <cstdio> header file.



Pointer to an array of characters where the proposed temporary name will be stored as a C string. The suggested size of this array is at least L_tmpnam characters.

Alternatively, a null pointer can be specified to use an internal static array to store the proposed temporary name, whose pointer is returned by the function.


Return Value:

On success, a pointer to the C string containing the proposed name for a temporary file:

1. If str was a null pointer, this points to an internal buffer (whose content is preserved at least until the next call to this function).

2. If str was not a null pointer, str is returned. If filename is not null, it returns filename.

3. If the function fails to create a suitable filename, it returns a null pointer. If any error occurs, null is returned.



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 (creating a unique directory using only the standard C library still requires the use of tmpnam).

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).   



Program 1: Example of tmpnam() function:


#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';




Output:  temporary file name: \salk.

          temporary file name: \salk.1



Program 2: Example of tmpnam() function:


#include <stdio.h>


int main ()


    char buffer [L_tmpnam];

    char * pointer;


    tmpnam (buffer);

    printf ("Tempname #1: %s\n",buffer);


    pointer = tmpnam (NULL);

    printf ("Tempname #2: %s\n",pointer);


    return 0;



Output: Tempname #1: \shpo.

          Tempname #2: \shpo.1



Program 3: Example of tmpnam() function:


#include <iostream>

#include <cstdio>


using namespace std;


int main ()


    char filename1[L_tmpnam],filename2[L_tmpnam];





    cout << "Temporary filenames:" << endl;

    cout << "1. " << filename1 << endl;

    cout << "2. " << filename2 << endl;


    char* filename3 = tmpnam(NULL);

    cout << "3. " << filename3;


  return 0;




Output:  Temporary filenames:

          1. \seak.

          2. \seak.1

          3. \seak.2