generate temporary file name 



#include <stdio.h>

char * tempnam(const char *dir, const char *pfx);


The tempnam() function generates a file name that can safely be used for a temporary file. This function finds the directory in which the file is to be created in the following order:

  1. If environment variable TMPDIR is set in the user's environment, tempnam() uses its value as the temporary file directory.
  2. If this is not set and the argument dir is not null, it uses dir as the directory.
  3. If dir is null or points to a string that is not a name for an appropriate directory, the path-prefix defined as P_tmpdir in the <stdio.h> header is used.
  4. If the directory specified in P_tmpdir is not accessible, / is used as a last resort.

The tempnam() function allocates space for the constructed file name, and returns a pointer to this area. Any pointer value returned from tempnam() may serve as an argument to free().

Within a single process, the function tempnam() generates a new name each time it is called.

Because tempnam() only generates a file name, rather than creating a file, it is possible that another application could create a file with the same name. The function tmpfile() is not vulnerable to this race condition.

The name returned by tempnam() is temporary only in the sense that it is not the name of an existing file. It is the user's responsibility to delete any files he creates with names returned by tempnam().



Is the name of the directory in which the file is to be created if TMPDIR is not defined.


Is the string to be used as the prefix of the temporary file name.


On success, tempnam() returns a pointer to a temporary file name, held in dynamically allocated storage. On failure, tempnam() returns a null pointer and sets errno to ENOMEM.


UNIX 98.




The function tempnam() returns a UNIX-style path, with the disk volume represented as a top-level directory. For a discussion of pathing issues, see the Windows Concepts section of the PTC MKS Toolkit UNIX to Windows Porting Guide.


PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


tmpfile(), tmpnam()

PTC MKS Toolkit 10.3 Documentation Build 39.