mktemp() Create a Unique File Name
#include <io.h> Required for declarations only
char *mktemp(template);
char *template; File name pattern
mktemp() creates a unique file name by modifying 'template'.
'template' has the form 'baseXXXXXX'. 'base' is the two-character
part of the new file name supplied by the user. The six Xs are
placeholders for the part supplied by mktemp(). 'base' is saved and
the six trailing Xs are replaced with an alphanumeric character
followed by a unique five-digit number.
Returns: A pointer to the modified template, if successful; or
null value if 'template' is badly formed or no more
unique names can be created.
Notes: mktemp() creates unique file names. It does not create or
open files.
In subsequent calls from the same program with the same
template, mktemp() checks to see if previously returned
names are now being used as filenames; if no file exists
for a given name, mktemp() returns that name. If files
exist for all previously returned names, mktemp() creates
a new name by replacing the alphanumeric character in the
name with the next available lowercase letter. So if the
first name returned is h0987654, and this name exists as
a file the next time mktemp() is called, ha987654 will be
returned. When creating names, mktemp() uses '0' and the
letters 'a' to 'z', in that order.
-------------------------------- Example ---------------------------------
The following statements use mktemp() to create a unique file name
and then use this name to open a new file.
#include <io.h>
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
char *template = "nfXXXXXX";
char *fname;
int fhndl;
main()
{
fname = mktemp(template);
fhndl = open(fname,O_CREAT,S_IREAD|S_IWRITE);
close(fhndl);
}
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster