Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

mkstemp - make unique temporary file name

&pagelevel(4)&pagelevel

Syntax

#include <stdlib.h>

int mkstemp(char *template);

Description mkstemp() creates a unique file name, normally in a temporary file system, and returns an

open file descriptor for this file. The file is opened for reading and writing.
In this way, mkstemp() prevents a possible race between an existence check and the
opening of the file.

The string to which template points should contain a file name followed by six Xs. mkstemp()
replaces these Xs with a letter and the current process ID to create a unique file name. The
letter is chosen so that the new file name will be unique, i.e. will not match any existing file
name.

Return val.

open file descriptor

if successful

-1

if no suitable file could be created.

Notes

It is possible that the letters may run out.

mkstemp() does not check whether the file name component in template exceeds the
maximum permitted length for file names.

For reasons of portability to implementations that comply with earlier versions of the
X/Open standard, the tmpfile() function is recommended for creating a unique file name.

mkstemp() changes the transferred string that is specified by template. This means that you
cannot use a string that is specified by template more than once. For each unique temporary
file you want to open, you need a new template.

If mkstemp() creates a new unique file name, the system first checks whether a file with this
name has already existed beforehand. Therefore, if you create more than one unique file
name, the same file name component should not be used in template for more than one
mkstemp() call.

See also

getpid(), open(), tmpfile(), tmpnam(), stdlib.h.