Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
ciso646 (iso646.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cstdio (stdio.h)
functions:
clearerr
fclose
feof
ferror
fflush
fgetc
fgetpos
fgets
fopen
fprintf
fputc
fputs
fread
freopen
fscanf
fseek
fsetpos
ftell
fwrite
getc
getchar
gets
perror
printf
putc
putchar
puts
remove
rename
rewind
scanf
setbuf
setvbuf
sprintf
sscanf
tmpfile
tmpnam
ungetc
vfprintf
vprintf
vsprintf
macro constants:
EOF
FILENAME_MAX
NULL
TMP_MAX
objects:
stderr
stdin
stdout
types:
FILE
fpos_t
size_t


tmpnam

function
<cstdio>
char * tmpnam ( char * str );

Generate temporary filename

A string containing a filename different from any existing file is generated.
This string can be used to create a temporary file without overwriting any other existing file.
If the str argument is a null pointer, the resulting string is stored in an internal static array that can be accessed by the return value. The content of this string is stored until a subsequent call to this same function erases it.
If the str argument is not a null pointer, it must point to an array of at least L_tmpnam bytes that will be filled with the proposed tempname. L_tmpnam is a macro constant defined in <cstdio>.
The file name returned by this function can be used to create a regular file using fopen to be used as a temp file. The file created this way, unlike those created with tmpfile is not automatically deleted when closed; You should call remove to delete this file once closed.

Parameters

str
Pointer to an array of chars where the proposed tempname will be stored as a C string. The size of this array should be at least L_tmpnam characters.
Alternativelly, a null pointer can be specified, in which case the string will be stored in an internal static array that can be accessed with the return value.

Return Value

A pointer to the C string containing the proposed name for a temporary file.
If str was a null pointer, this points to an internal buffer that will be overwritten the next time this function is called.
If str was not a null pointer, str is returned.
If the function fails to create a suitable filename, it returns a null pointer.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* tmpnam example */
#include <stdio.h>
int main ()
{
  char buffer [L_tmpnam];
  char * pointer;
  tmpnam (buffer);
  printf ("Tempname #1: %s\n",buffer);
  pointer = tmpnam (NULL);
  printf ("Tempname #2: %s\n",pointer);
  return 0;  
}


This program will generate two different names for temporary files. Each one has been created by one of the two methods in which tmpnam can be used.

See also