Defined in header <stdio.h> | ||
---|---|---|
char *tmpnam( char *filename ); | (1) | |
errno_t tmpnam_s(char *filename_s, rsize_t maxsize); | (2) | (since C11) |
L_tmpnam
in length) and stores it in character string pointed to by filename
. The function is capable of generating up to TMP_MAX
of unique filenames, but some or all of them may be in use in the filesystem and thus not suitable return values. TMP_MAX_S
names may be generated, no longer than L_tmpnam)s
in length, and he following errors are detected at runtime and call the currently installed constraint handler function: -
-
filename_s
is a null pointer -
maxsize
is greater thanRSIZE_MAX
-
maxsize
is less than the generated file name string
-
- As all bounds-checked functions,
tmpnam_s
is only guaranteed to be available of__STDC_LIB_EXT1__
is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__
to the integer constant1
before including<stdio.h>
.
tmpnam
and tmpnam_s
modify static state (which may be shared between these functions) and are not required to be thread-safe.
Parameters
filename | - | pointer to the character array capable of holding at least L_tmpnam bytes, to be used as a result buffer. If null pointer is passed, a pointer to an internal static buffer is returned. |
filename_s | - | pointer to the character array capable of holding at least L_tmpnam_s bytes, to be used as a result buffer. |
maxsize | - | maximum number of characters the function is allowed to write (typically the size of the filename_s array). |
Return value
filename
if filename
was not a null pointer. Otherwise a pointer to an internal static buffer is returned. If no suitable filename can be generated, null pointer is returned.filename_s
on success. On error, returns non-zero and writes the null character to filename_s[0]
(only if filename_s
is not null and maxsize
is not zero and is not greater than RSIZE_MAX
).Notes
Although the names generated by tmpnam
are difficult to guess, it is possible that a file with that name is created by another process between the moment tmpnam
returns and the moment this program attempts to use the returned name to create a file. The standard function tmpfile
and the POSIX function mkstemp do not have this problem (creating a unique directory using only the standard C library still requires the use of tmpnam
).
POSIX systems additionally define the similarly named function tempnam()
, which offers the choice of a directory (which defaults to the optionally defined macro P_tmpdir)
.
Example
#include <stdio.h> #include <string.h> int main(void) { char* name1 = tmpnam(NULL); printf("temporary file name: %s\n", name1); char name2[L_tmpnam]; if (tmpnam(name2)) printf("temporary file name: %s\n", name2); }
Possible output:
temporary file name: /tmp/fileRZHMwL temporary file name: /tmp/file420gSN
References
- C11 standard (ISO/IEC 9899:2011):
- 7.21.4.4 The tmpnam function (p: 303-304)
- K.3.5.1.2 The tmpnam_s function (p: 587-588)
- C99 standard (ISO/IEC 9899:1999):
- 7.19.4.4 The tmpnam function (p: 269-270)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.9.4.4 The tmpnam function
See also
(C11) | returns a pointer to a temporary file (function) |
C++ documentation for tmpnam |
Please login to continue.