tmpnam() — Produce temporary file name

Standards

Standards / Extensions C or C++ Dependencies

ISO C
POSIX.1
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3

both  

Format

#include <stdio.h>

char *tmpnam(char *string);

General description

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 a NULL pointer, tmpnam() leaves the result in an internal static buffer. Any subsequent calls may modify this object. If string is not a NULL pointer, it must point to an array of at least L_tmpnam bytes. The value of L_tmpnam is defined in the stdio.h header file.

Returned value

If string is a NULL pointer, tmpnam() returns the pointer to the internal static object in which the generated unique name is placed. Otherwise, if string is not a NULL pointer, it returns the value of string. The tmpnam() function produces a different name each time it is called within a module up to at least TMP_MAX names. Files created using names returned by tmpnam() are not automatically discarded at the end of the program.

Returned value for POSIX C

The tmpnam() function behaves differently when a program is running with POSIX(ON).

When the __POSIX_TMPNAM environment variable is unset or is not set to NO, the file name returned is a unique file name in the UNIX file system. The directory component of the file name will be the value of the TMPDIR environment variable, or '/tmp' if TMPDIR is not defined.

When the __POSIX_TMPNAM environment variable is set to NO, the file name returned is as if the calling application were running POSIX(OFF).

Example

CELEBT14
/* CELEBT14                                      

   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