| Updated: October 28, 2024 |
Make a unique temporary filename and open the file
#include <stdlib.h>
int mkstemp( char* template );
int mkstemps( char *template,
int slen );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The mkstemp() and mkstemps() functions take the given file name template and overwrite a portion of it to create a filename.
The generated file name is unique and suitable for use by the application. The Xs are replaced with the current process number and/or a unique letter combination. The number of unique file names that these functions can return depends on the number of Xs provided; the functions try at least 231 combinations before giving up.
The mkstemp() and mkstemps() functions (unlike mktemp()) create the template file, mode 0600 (i.e., read-write for the owner), returning a file descriptor opened for reading and writing. This avoids the race between testing for a file's existence and opening it for use.
The file descriptor of the temporary file, or -1 if no suitable file could be created (errno is set).
This function may also set errno to any value specified by open().
mkstemp() is POSIX 1003.1; mkstemps() is OpenBSD
| Safety: | |
|---|---|
| Cancellation point | Yes |
| Interrupt handler | No |
| Signal handler | Yes |
| Thread | Yes |