tmpnam() — Produce Temporary File Name

Format

#include <stdio.h>
char *tmpnam(char *string);

Language Level

ANSI

Threadsafe

Yes

However, using tmpnam(NULL) is NOT threadsafe.

Description

The tmpnam() function produces a valid file name that is not the same as the name of any existing file. It stores this name in string. If string is NULL, the tmpnam() function leaves the result in an internal static buffer. Any subsequent calls destroy this value. If string is not NULL, it must point to an array of at least L_tmpnam bytes. The value of L_tmpnam is defined in <stdio.h>.

The tmpnam() function produces a different name each time it is called within an activation group up to at least TMP_MAX names. For ILE C, TMP_MAX is 32 767. This is a theoretical limit; the actual number of files that can be opened at the same time depends on the available space in the system.

Return Value

The tmpnam() function returns a pointer to the name. If it cannot create a unique name then it returns NULL.

Example

This example calls tmpnam() to produce a valid file name.
#include <stdio.h>
 
int main(void)
{
   char *name1;
   if ((name1 = tmpnam(NULL)) !=NULL)
      printf("%s can be used as a file name.\n", name1);
   else
      printf("Cannot create a unique file name\n");
}

Related Information