正文

C语言中非常有用的函数mktemp2007-02-13 12:32:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/redstar/23330.html

分享到:

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

阅读(3520) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册