_tempnam, tmpnam

Description

Create temporary filenames.

#include <stdio.h>

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

char *tmpnam( char *string );

string Pointer to temporary name  
dir Target directory to be used if TMP not defined  
prefix Filename prefix  

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 bytes (the value of L_tmpnam is defined in STDIO.H). The function will generate 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 can range from 1 to 65,535. Changing the definitions of L_tmpnam or P_tmpdir in STDIO.H does not change the operation of tmpnam.

The _tempnam function allows the program to create a temporary filename for use in another directory. This filename will be different from that of any existing file. The prefix argument is the prefix to the filename. The _tempnam function uses malloc to allocate space for the filename; the program is responsible for freeing this space when it is no longer needed. The _tempnam function 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 The dir argument is NULL, or dir is name of nonexistent directory.
Current working directory P_tmpdir does not exist.

If the search through the locations listed above fails, _tempnam returns the value NULL.

Return Value

The tmpnam and _tempnam functions both return 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 the value NULL.

Compatibility

_tempnam

Standards:UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

Use _tempnam for compatibility with ANSI naming conventions of non-ANSI functions. Use tempnam and link with OLDNAMES.LIB for UNIX compatibility.

tmpnam

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

See Also

tmpfile

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 file name 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

\2 is safe to use as a temporary file.

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