char *mktemp( char *template );
用于:Create a unique filename.
Windows中,函数叫下面两个名字,包含于 io.h 文件中
char *_mktemp( char *template );
wchar_t *_wmktemp( wchar_t *template );
Return Value
Each of these functions returns a pointer to the modified template. The function returns NULL if template is badly formed or no more unique names can be created from the given template.
template的格式:baseXXXXXX,base为任意字符,然后是六个X。(记住,是六个哦)
Example
/* MKTEMP.C: The program uses _mktemp to create * five unique filenames. It opens each filename * to ensure that the next name is unique. */ #include"stdio.h"
#include "io.h" #include "string.h" char *template = "fnXXXXXX"; char *result; char names[5][9]; void main( void ) { int i; FILE *fp; for( i = 0; i < 5; i++ ) { strcpy( names[i], template ); /* Attempt to find a unique filename: */ result = _mktemp( names[i] ); if( result == NULL ) printf( "Problem creating the template" ); else { if( (fp = fopen( result, "w" )) != NULL ) printf( "Unique filename is %s\n", result ); else printf( "Cannot open %s\n", result ); fclose( fp ); } } }
Output
Unique filename is fna00141
Unique filename is fnb00141
Unique filename is fnc00141
Unique filename is fnd00141
Unique filename is fne00141
评论