C Standard Library

C <stdio.h> - tmpname() Function



The C <stdio.h> tmpname() function creates a string containing a file name different from the name of any existing file, and stores it in the character string pointed to by str. The function is capable of generating up to TMP_MAX of unique filenames, but some or all of them may already be in use. If it is called more than TMP_MAX times, the behavior is implementation dependent.

Syntax

char * tmpnam ( char * str );

Parameters

str Specify a pointer to the character array where the proposed temporary name will be stored as a C string. The size of this array should be at least L_tmpnam bytes. If a null pointer is passed, a pointer to an internal static buffer is returned.

Return Value

On success, returns str if str is not a null pointer, Otherwise a pointer to an internal static buffer is returned. On failure, a null pointer is returned.

Example:

The example below shows the usage of tmpnam() function.

#include <stdio.h>
 
int main (){

  //creating file name by passing null pointer
  char * filename1 = tmpnam(NULL);
  printf("Temporary file name: %s\n", filename1);

  //creating file name by passing character string
  char filename2[L_tmpnam];
  tmpnam(filename2);
  printf("Temporary file name: %s\n", filename2);

  return 0;
}

One of the possible outcome of the above code could be:

Temporary file name: /tmp/filepTvks2
Temporary file name: /tmp/filepDFZI7

❮ C <stdio.h> Library