请输入您要查询的百科知识:

 

词条 mktemp
释义

C语言创建唯一临时文件函数mktemp

函数简介

头文件:在TC2.0中其头文件是dir.h,在Visual C++ 6.0中,更多的使用_mktemp,其头文件是io.h

定义函数 char * mktemp(char * template);

功能: mktemp()用来产生唯一的临时文件名。参数template所指的文件名称字符串中最后六个字符必须是XXXXXX。产生后的文件名会借字符串指针返回。

返回值: 文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回NULL,并把错误代码存在errno中。

其他创建临时文件的函数:tmpfile,tmpnam程序示例

程序示例1

(在Visual C++ 6.0中运行通过)

#include<stdio.h>

#include<string.h>

#include<io.h>

int main()

{

char *templat="fnXXXXXX";

char *result;

char names[5][9];

int Count ;

for(Count=0; Count<5;Count++)

{

strcpy(names[Count],templat);

result=_mktemp(names[Count]);

printf("%s\\\r",result);

}

return 0;

}

程序例2

(在TC2.0中运行成功)

#include <stdio.h>

#include <dir.h>

int main(void)

{

FILE *fp;

char *fname = "d:\\\\TXXXXXX", *newname, first;

newname = mktemp(fname);

fp = fopen(newname,"w+");

fprintf(fp,"abcdefghijklmnopqrstuvwxyz");

rewind(fp);

fscanf(fp,"%c",&first);

printf("The first character is: %c\",first);

fclose(fp);

remove(newname);

return 0;

}

附加说明 参数template所指的文件名称字符串必须声明为数组,如:

char template[ ]=”template-XXXXXX”;

不可用char * template=”template-XXXXXX”;

范例 #include&lt;stdlib.h&gt;

main()

{

char template[ ]=”template-XXXXXX”;

mktemp(template);

printf(“template=%s\”,template);

}

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/2/12 12:07:44