Skip to content
Search

C:tmpnam

lastmod 2018-08-17created 2016-01-0910 edits

Example

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

#include <stdio.h>

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

   tmpnam(buffer);
   printf("Temporary name 1: %s\n", buffer);

   ptr = tmpnam(NULL);
   printf("Temporary name 2: %s\n", ptr);

   return(0);
}

Let us compile and run the above program to produce the following result:

Temporary name 1: /tmp/filebaalTb
Temporary name 2: /tmp/filedCIbb0

tmpnam is dangerous

tmpnam() is a dangerous function in terms of security. (Another process can hijack the handle the moment the function name is returned.) POSIX recommends using mkstemp instead.

warning: the use of `tmpnam' is dangerous, better use `mkstemp'

See also

Favorite site