generate a unique string for use as a file name
#include <stdio.h> char *tmpnam( char *buffer );
The tmpnam() function generates a unique string for use as a valid file name.
If the TMPDIR environment variable is defined, its value is used as a prefix for the temporary file name. If TMPDIR isn't defined, the path /tmp is used as a prefix for the temporary file name. In either case, if the path doesn't exist, the current directory (“.”) is used. The filename component has the format UUUPPPP.NNNN.TMP, where:
For example, if the process ID is 0x0056 and the network ID is 0x0234 then the first temporary file name produced resembles one of the following:
{TMPDIR_string}/AAAFG.BCD.TMP
/tmp/AAAFG.BCD.TMP
./AAAFG.BCD.TMP
Subsequent calls to tmpnam() reuse the internal buffer.
If the argument buffer is a NULL pointer, tmpnam() returns a pointer to an internal buffer containing the temporary file name. If the argument buffer isn't a NULL pointer, tmpnam() copies the temporary file name from the internal buffer to the specified buffer, and returns a pointer to the specified buffer. It is assumed that the specified buffer is an array of at least L_tmpnam characters.
char *name1, *name2; name1 = strdup( tmpnam( NULL ) ); name2 = strdup( tmpnam( NULL ) );
#include <stdio.h>
void main()
{
char filename[ L_tmpnam ];
FILE *fp;
tmpnam( filename );
fp = fopen( filename, "w+b" );
/* . */
/* . */
/* . */
fclose( fp );
remove( filename );
}
ANSI
All (except DOS/PM)