Unix to Windows Porting Dictionary for HPC

Links

Function List

mkstemp


Unix

header file: stdlib.h

int mkstemp (char *template);
char *mktemp(char *template);
int mkstemps(char *template, int suffixlen);
char *mkdtemp(char *template);

Windows

header file: io.h, string.h

errno_t _mktemp_s(char *template, size_t sizeInChars);
errno_t _wmktemp_s(wchar_t *template, size_t sizeInChars);

Purpose

Make a (unique) temporary filename.

Discussion

These Unix functions are used to create unique filenames based on a template provided by the program. This allows the program to reduce the possibility of filename collision with another program creating (or have created) a temporary file with the same name. The mkstemp() function differs from mktemp() by actually creating a file and ensuring the main program reduces significantly the possibility of the function not being successful. The mkdtemp() function is like mkstemp() but for directories instead of files. The mkstemps() function is a variation of mkstemp() that allows for a suffix to the filename. Both mkstemp() and mkstemps() return a file descriptor that can be used immediately.

Windows has only the _mkstemp_s() and _wmktemp_s() [wide character version] functions which are equivalent to the Unix mktemp() function. These Windows functions also require the size of the template (to avoid overflow). The Unix mkstemp() function is very useful and is the more recommended function to use over mktemp(). It is better when porting source code from Unix to Windows to create a helper function for mkstemp() to maintain program behavior. Sample code for a Windows version of mkstemp() is freely available at the ftp link listed below and returns a Windows file handle.

Example of Use in Windows

#include <io.h>
#include <string.h>

char *template = "tmpXXXXXX";
char name[9]; /* size of template including trailing nul */
int rez;

strcpy_s(name, sizeof(name), template);
rez = _mktemp_s(name, sizeof(name));
if (rez == 0) {
        /* template created successfully */
}
else {
        /* not successful */
}

  
blog comments powered by Disqus