mkstemp(), mkstemps()

Make a unique temporary filename and open the file


#include <stdlib.h>

int mkstemp( char* template );

int mkstemps( char *template,
              int slen );


A template for the filename that you want to use. For mkstemp(), this template can be any file name with at least six X characters appended to it (for example, /tmp/temp.XXXXXX); for mkstemps(), it also includes a suffix (e.g., /tmp/temp.XXXXXXsuffix).
(mkstemps() only) The length of the suffix included in the template.



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).


The template doesn't include at least six X wildcard characters, or (for mkstemps()) the length of the template is less than slen.
All the names tried already exist. Consider adding more wildcard characters to the template.

This function may also set errno to any value specified by open().


mkstemp() is POSIX 1003.1; mkstemps() is OpenBSD

Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes