C Library Functions -tmpnam()

C Standard Library - <stdio.h>

Description

C Library Functionschar *tmpnam(char *str)Generate and return a valid temporary filename that did not previously exist. IfstrIf it is empty, only the temporary filename will be returned.

Declaration

The following is the declaration of the tmpnam() function.

char *tmpnam(char *str)

Parameter

  • str-- This is a pointer to a character array in which the temporary filename will be stored as a C string.

Return Value

  • A pointer to a C string that stores the temporary filename. If str is a null pointer, this pointer points to an internal buffer that will be overwritten on the next call to the function.
  • If str is not a null pointer, str is returned. If the function fails to successfully create a usable filename, a null pointer is returned.

Example

The following example demonstrates the usage of the tmpnam() function.

#include <stdio.h>

int main()
{
   char buffer[L_tmpnam];
   char *ptr;


   tmpnam(buffer);
   printf("临时名称 1: %s\n", buffer);
 
   ptr = tmpnam(NULL);
   printf("临时名称 2: %s\n", ptr);

   return(0);
}

Let us compile and run the above program, which will produce the following result:

临时名称 1: /tmp/filebaalTb
临时名称 2: /tmp/filedCIbb0

C Standard Library - <stdio.h>

other extensions