### tempnam -- Produce Temporary File Name

Format

>     #include <stdio.h>
>     char *tempnam(char *dir, char *prefix);

Language Level: XPG4, Extension
tempnam creates a temporary file name in another directory. The prefix is the prefix to the file name. tempnam tests for the existence of the file with the given name in the following directories, listed in order of precedence:

- If the TMP environment variable is set and the directory specified by TMP exists, the directory is specified by TMP.
- If the TMP environment variable is not set or the directory specified by TMP does not exist, the directory is specified by the dir argument to tempnam.
- If the dir argument is NULL, or dir is the name of nonexistent directory, the directory is pointed to by P_tmpdir (defined in <stdio.h>).
- If P_tmpdir does not exist, the directory is the current working directory.

Notes:

1. Because tempnam uses malloc to reserve space for the created file name, you must free this space when you no longer need it.
2. In earlier releases of the C/C++ run-time library, tempnam began with an underscore (_tempnam). Because it is defined by the X/Open standard, the underscore has been removed. For compatibility, IBM C and C++ Compilers will map _tempnam to tempnam for you.

Return Value
tempnam returns a pointer to the temporary name, if successful. If the name cannot be created or it already exists, tempnam returns the value NULL.

Example
This example creates a temporary file name using the directory d:\tmp:

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

    int main(void)
    {
       char *name1;

       if ((name1 = tempnam("d:\\tmp", "stq")) != NULL)
          printf("%s is safe to use as a temporary file.\n", name1);
       else {
          printf("Cannot create unique filename\n");
          return EXIT_FAILURE;
       }
       return 0;

       /**************************************************************
          The output should be similar to:

          D:\tmp\stqU3CP2.C2T is safe to use as a temporary file.
       **************************************************************/
    }

malloc -- Reserve Storage Block
_rmtmp -- Remove Temporary Files
tmpfile -- Create Temporary File
tmpnam -- Produce Temporary File Name
<stdio.h>