mkdtemp(), mkstemp(), mkstemp64(), mkstemps(), mkstemps64), mktemp()

create a temporary file 

Function


SYNOPSIS

#include <stdlib.h>

char *mkdtemp(char *template);

int mkstemp(char *template);

int mkstemp64(char *template);

int mkstemps(char *template, int suffixlen);

int mkstemps64(char *template, int suffixlen);

char *mktemp(char *template);


DESCRIPTION

The mktemp() function returns a unique file name based on the template parameter. At the time it is generated, no file in the current directory has that name. No file is actually created, so it is possible that another application could create a file with this name.

The mkstemp() function is similar to mktemp() in that it create a unique file name based on template; however, mkstemp() actually creates the file and returns its file descriptor. The name of the created file is stored in template.

The mkstemp64() function is identical to the mkstemp() function except that the file is opened with the O_LARGEFILE flag set.

The mkstemps() function is identical to the mkstemps() function except that template can contain a fixed suffix. The length of this suffix is given by the suffixlen parameter.

The mkstemps64() function is identical to the mkstemps() except that the file is opened with the O_LARGEFILE flag set.

The mkdtemp() function is similar to mktemp() except that the name generated is treated as a directory name which is then created. Like mktemp(), this function returns the generated name.

The template parameter points to a character buffer containing a null-terminated string. This string consists of contiguous, legal file name characters followed by a contiguous string of Xs. The functions described on this page replace the trailing Xs with an alphanumeric sequence that is chosen to ensure that no file has that name. When there is no path information in template, the current directory is assumed to be where the generated file name will reside. Xs that appear elsewhere in the string are not replaced.

For the mkstemps() and mkstemps64() functions, suffixlen characters are ignored when determining whether the Xs are trailing or not. For example,

char template[]="tmpfileXXX.txt"
mkstemps(template,4) 

ignores the final four characters of the strings (in this case, the .txt extension) and replaces the three trailing Xs.

Note:

When using this functionality to specify file extensions as a part of the generated name, the period (.) counts as one of the characters to be ignored. That's why the example above used a suffixlen of 4 to include the .txt extension.

However, consider this case:

char template[]="tmpfileXXX.Xxt"
mkstemps(template,2)

Here, mkstemps() only replaces the X in the extension when generating a unique file name. The suffixlen of 2 tells it to ignore the xt when looking for trailing Xs and the period (.) separates the X in the extension from the others, meaning there is only the one trailing X to be replaced.


PARAMETERS

template 

Points to a buffer containing a null-terminated string used as the template for generating a unique name.

For the mkstemp(), mkstemp64(), mkstemps(), mkstemps64(), this buffer is also used to store the resulting unique file name.

suffixlen 

Specifies the length of the suffix to be included in the file name generated by mkstemps() or mkstemps64().


RETURN VALUES

On success, mktemp() and mkdtemp() return a pointer to the template buffer, which contains the generated file or directory name. If these functions cannot generate a unique name, they return a null pointer and mkdtemp() sets errno to reflect the error that occurred.

On success, mkstemp(), mkstemp64(), mkstemps(), and mkstemps64() return a file descriptor to the newly created file and load the template buffer with the name of this file. In the event that these functions cannot generate a unique name, they return -1 and set errno to reflect the error that occurred.


CONFORMANCE

UNIX 98.


MULTITHREAD SAFETY LEVEL

MT-Safe.


PORTING ISSUES

None.


AVAILABILITY

PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
open(), tempnam(), tmpfile(), tmpnam()


PTC MKS Toolkit 10.3 Documentation Build 39.