Export (0) Print
Expand All
Expand Minimize
2 out of 2 rated this helpful - Rate this topic

_tempnam, _wtempnam, tmpnam, _wtmpnam

Generate names you can use to create temporary files.

char *_tempnam(
   const char *dir,
   const char *prefix 
);
wchar_t *_wtempnam(
   const wchar_t *dir,
   const wchar_t *prefix 
);
char *tmpnam(
   char *string 
);
wchar_t *_wtmpnam(
   wchar_t *string 
);

Parameters

prefix
The string that will be prepended to names returned by _tempnam.
dir
The path used in the file name if there is no TMP environment variable, or if TMP is not a valid directory.
string
Pointer that will hold the generated name and will be identical to the name returned by the function. This is a convenient way to save the generated name.

Return Value

Each of these functions returns a pointer to the name generated or NULL if there is a failure. Failure can occur if you attempt more than TMP_MAX (see STDIO.H) calls with tmpnam or if you use _tempnam and there is an invalid directory name specified in the TMP environment variable and in the dir parameter.

Note   The pointers returned by tmpnam and _wtmpnam point to internal static buffers. free does not need to be called to deallocate those pointers. free needs to be called for pointers allocated by _tempnam and _wtempnam.

Remarks

Each of these functions returns the name of a file that does not currently exist. tmpnam returns a name unique in the current working directory and _tempnam lets you generate a unique name in a directory other than the current one. Note than when a file name is prepended with a back slash and no path information, such as \fname21, this indicates that the name is valid for the current working directory.

For tmpnam, you can store this generated file name in string. If string is NULL, then tmpnam leaves the result in an internal static buffer. Thus any subsequent calls destroy this value. The name generated by tmpnam consists of a program-generated file name and, after the first call to tmpnam, a file extension of sequential numbers in base 32 (.1-.vvu, when TMP_MAX in STDIO.H is 32,767).

_tempnam will generate a unique file name for a directory chosen by the following rules:

  • If the TMP environment variable is defined and set to a valid directory name, unique file names will be generated for the directory specified by TMP.
  • If the TMP environment variable is not defined or if it is set to the name of a directory that does not exist, _tempnam will use the dir parameter as the path for which it will generate unique names.
  • If the TMP environment variable is not defined or if it is set to the name of a directory that does not exist, and if dir is either NULL or set to the name of a directory that does not exist, _tempnam will use the current working directory to generate unique names. Currently, if both TMP and dir specify names of directories that do not exist, the _tempnam function call will fail.

The name returned by _tempnam will be a concatenation of prefix and a sequential number, which will combine to create a unique file name for the specified directory. _tempnam generates file names that have no extension. _tempnam uses malloc to allocate space for the filename; the program is responsible for freeing this space when it is no longer needed.

_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

Requirements

Routine Required header Compatibility
_tempnam <stdio.h> Win 98, Win Me, Win NT, Win 2000, Win XP
_wtempnam <stdio.h> or <wchar.h> Win 98, Win Me, Win NT, Win 2000, Win XP
tmpnam <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
_wtmpnam <stdio.h> or <wchar.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_tmpnam.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>
#include <stdlib.h>

int main( void )
{   
   char* name1 = NULL;
   char* name2 = NULL;

   /* 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" );

   // When name2 is no longer needed :   
   if(name2)
     free(name2);
     
}

Sample Output

\s1gk. is safe to use as a temporary file.
C:\DOCUME~1\user\LOCALS~1\Temp\2\stq2 is safe to use as a temporary file.

See Also

Stream I/O Routines | _getmbcp | malloc | _setmbcp | tmpfile | Run-Time Routines and .NET Framework Equivalents

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.