| Types and objects | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <stdio.h> | ||
char*tmpnam(char*filename); | (1) | |
errno_t tmpnam_s(char*filename_s, rsize_t maxsize); | (2) | (since C11) |
#define TMP_MAX /*unspecified*/ | ||
#define TMP_MAX_S /*unspecified*/ | (since C11) | |
#define L_tmpnam /*unspecified*/ | ||
#define L_tmpnam_s /*unspecified*/ | (since C11) | |
tmpnam_s is only guaranteed to be available if__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 andtmpnam_s modify static state (which may be shared between these functions) and are not required to be thread-safe.
Contents |
| filename | - | pointer to the character array capable of holding at leastL_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 leastL_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 thefilename_s array). |
Although the names generated bytmpnam are difficult to guess, it is possible that a file with that name is created by another process between the momenttmpnam returns and the moment this program attempts to use the returned name to create a file. The standard functiontmpfile and the POSIX functionmkstemp do not have this problem (creating a unique directory using only the standard C library still requires the use oftmpnam).
POSIX systems additionally define the similarly named functiontempnam, which offers the choice of a directory (which defaults to the optionally defined macroP_tmpdir).
#include <stdio.h>#include <stdlib.h>#include <string.h> int main(void){// Note, the compiler/linker may issue a security warning, e.g. GCC:// "warning: the use of `tmpnam' is dangerous, better use `mkstemp'"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); // POSIX offers mkstemp. The following declaration might be// necessary as mkstemp is absent in the standard C <stdlib.h>.int mkstemp(char*); char name3[]="/tmp/fileXXXXXX";// at least six 'X' required ^_^int file_descriptor= mkstemp(name3);if(file_descriptor!=-1)printf("temporary file name: %s\n", name3);elseperror("mkstemp");}
Possible output:
temporary file name: /tmp/file90dLlRtemporary file name: /tmp/fileY9LWAgtemporary file name: /tmp/filexgv8PF
(C11) | returns a pointer to a temporary file (function)[edit] |
C++ documentation fortmpnam | |