Edit

Share via


tmpnam_s, _wtmpnam_s

Generate names you can use to create temporary files. These functions are versions of tmpnam and _wtmpnam with security enhancements as described in Security features in the CRT.

Syntax

errno_t tmpnam_s(
   char * str,
   size_t sizeInChars
);
errno_t _wtmpnam_s(
   wchar_t *str,
   size_t sizeInChars
);
template <size_t size>
errno_t tmpnam_s(
   char (&str)[size]
); // C++ only
template <size_t size>
errno_t _wtmpnam_s(
   wchar_t (&str)[size]
); // C++ only

Parameters

str
[out] Pointer that holds the generated name.

sizeInChars
[in] The size of the buffer in characters.

Return value

Both of these functions return 0 if successful or an error number on failure.

Error conditions

str sizeInChars Return value Contents of str
NULL any EINVAL not modified
not NULL (points to valid memory) too short ERANGE not modified

If str is NULL, the invalid parameter handler is invoked, as described in Parameter validation. If execution is allowed to continue, these functions set errno to EINVAL and return EINVAL.

Remarks

Each of these functions returns the name of a file that doesn't currently exist. tmpnam_s returns a name unique in the designated Windows temporary directory returned by GetTempPathW. When a file name is prepended with a backslash and no path information, such as \fname21, it indicates that the name is valid for the current working directory.

For tmpnam_s, you can store this generated file name in str. The maximum length of a string returned by tmpnam_s is L_tmpnam_s, defined in STDIO.H. If str is NULL, then tmpnam_s leaves the result in an internal static buffer. Thus any subsequent calls destroy this value. The name generated by tmpnam_s consists of a program-generated file name and, after the first call to tmpnam_s, a file extension of sequential numbers in base 32 (.1-.1vvvvvu, when TMP_MAX_S in STDIO.H is INT_MAX).

tmpnam_s automatically handles multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the OEM code page obtained from the operating system. _wtmpnam_s is a wide-character version of tmpnam_s; the argument and return value of _wtmpnam_s are wide-character strings. _wtmpnam_s and tmpnam_s behave identically except that _wtmpnam_s doesn't handle multibyte-character strings.

In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically, eliminating the need to specify a size argument. For more information, see Secure template overloads.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Generic-text routine mappings

TCHAR.H routine _UNICODE and _MBCS not defined _MBCS defined _UNICODE defined
_ttmpnam_s tmpnam_s tmpnam_s _wtmpnam_s

Requirements

Routine Required header
tmpnam_s <stdio.h>
_wtmpnam_s <stdio.h> or <wchar.h>

For more compatibility information, see Compatibility.

Example

// crt_tmpnam_s.c
// This program uses tmpnam_s to create a unique filename in the
// current working directory.
//

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
   char name1[L_tmpnam_s];
   errno_t err;
   int i;

   for (i = 0; i < 15; i++)
   {
      err = tmpnam_s( name1, L_tmpnam_s );
      if (err)
      {
         printf("Error occurred creating unique filename.\n");
         exit(1);
      }
      else
      {
         printf( "%s is safe to use as a temporary file.\n", name1 );
      }
   }
}
C:\Users\LocalUser\AppData\Local\Temp\u19q8.0 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.1 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.2 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.3 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.4 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.5 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.6 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.7 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.8 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.9 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.a is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.b is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.c is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.d is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.e is safe to use as a temporary file.

See also

Stream I/O
_getmbcp
malloc
_setmbcp
tmpfile_s