mkstemp() — Make a unique filename

Standards

Standards / Extensions C or C++ Dependencies

XPG4.2
Single UNIX Specification, Version 3

both POSIX(ON)

Format

#define _XOPEN_SOURCE_EXTENDED 1
#include <stdlib.h>

int mkstemp(char *template);

General description

The mkstemp() function replaces the contents of the string pointed to by template with a unique file name, and returns a file descriptor for the file open for reading and writing. The function thus prevents any possible race condition between testing whether the file exists and opening it for use. The string in template should look like a file name with six trailing 'X's; mkstemp() replaces each 'X' with a character from the portable file name character set. The characters are chosen such that the resulting name does not duplicate the name of an existing file. This function is supported only in a POSIX program.

Returned value

If successful, mkstemp() returns an open file descriptor.

If no suitable file could be created, mkstemp() returns -1.

There are no errno values defined.

Related information