Share via


_tempnam, _wtempnam, tmpnam, _wtmpnam

Create temporary filenames.

char*_tempnam(char*dir,char*prefix);

wchar_t*_wtempnam(wchar_t*dir,wchar_t*prefix);

char*tmpnam(char*string);

wchar_t*_wtmpnam(wchar_t*string);

Routine Required Header Compatibility
_tempnam <stdio.h> Win 95, Win NT
_wtempnam <stdio.h> or <wchar.h> Win 95, Win NT
tmpnam <stdio.h> ANSI, Win 95, Win NT
_wtmpnam <stdio.h> or <wchar.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

Each of these functions returns a pointer to the name generated, unless it is impossible to create this name or the name is not unique. If the name cannot be created or if a file with that name already exists, tmpnam and _tempnam return NULL. _tempnam and _wtempnam also return NULL if the file search fails.

Note   The pointer returned by tmpnam points to an internal static buffer. free does not need to be called to deallocate this pointer.

Parameters

prefix

Filename prefix

dir

Target directory to be used if TMP not defined

string

Pointer to temporary name

Remarks

The tmpnam function generates a temporary filename that can be used to open a temporary file without overwriting an existing file.

This name is stored in string. If string is NULL, then tmpnam leaves the result in an internal static buffer. Thus any subsequent calls destroy this value. If string is not NULL, it is assumed to point to an array of at least L_tmpnam characters — or 2*L_tmpnam bytes. The value of L_tmpnam is defined in STDIO.H. The function generates unique filenames for up to TMP_MAX calls.

The character string that tmpnam creates consists of the path prefix, defined by the entry P_tmpdir in the file STDIO.H, followed by a sequence consisting of the digit characters '0' through '9'; the numerical value of this string is in the range 1 – 65,535. Changing the definitions of L_tmpnam or P_tmpdir in STDIO.H does not change the operation of tmpnam.

_tempnam creates a temporary filename for use in another directory. This filename is different from that of any existing file. The prefix argument is the prefix to the filename. _tempnam uses malloc to allocate space for the filename; the program is responsible for freeing this space when it is no longer needed. _tempnam looks for the file with the given name in the following directories, listed in order of precedence.

Directory Used Conditions
Directory specified by TMP TMP environment variable is set, and directory specified by TMP exists.
dir argument to _tempnam TMP environment variable is not set, or directory specified by TMP does not exist.
P_tmpdir in STDIO.H dir argument is NULL, or dir is name of nonexistent directory.
Current working directory P_tmpdir does not exist.

_tempnam and tmpnam automatically handle multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the OEM code page obtained from the operating system. _wtempnam is a wide-character version of _tempnam; the arguments and return value of _wtempnam are wide-character strings. _wtempnam and _tempnam behave identically except that _wtempnam does not handle multibyte-character strings. _wtmpnam is a wide-character version of tmpnam; the argument and return value of _wtmpnam are wide-character strings. _wtmpnam and tmpnam behave identically except that _wtmpnam does not handle multibyte-character strings.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_ttmpnam tmpnam tmpnam _wtmpnam
_ttempnam _tempnam _tempnam _wtempnam

Example

/* TEMPNAM.C: This program uses tmpnam to create a unique
 * filename in the current working directory, then uses
 * _tempnam to create a unique filename with a prefix of stq.
 */

#include <stdio.h>

void main( void )
{
   char *name1, *name2;

   /* Create a temporary filename for the current working directory: */
   if( ( name1 = tmpnam( NULL ) ) != NULL )
      printf( "%s is safe to use as a temporary file.\n", name1 );
   else
      printf( "Cannot create a unique filename\n" );

   /* Create a temporary filename in temporary directory with the
    * prefix "stq". The actual destination directory may vary
    * depending on the state of the TMP environment variable and
    * the global variable P_tmpdir.
    */
   if( ( name2 = _tempnam( "c:\\tmp", "stq" ) ) != NULL )
      printf( "%s is safe to use as a temporary file.\n", name2 );
   else
      printf( "Cannot create a unique filename\n" );
}

Output

\s5d. is safe to use as a temporary file.
C:\temp\stq2 is safe to use as a temporary file.

Stream I/O Routines

See Also   _getmbcp, malloc, _setmbcp, tmpfile