mkstemp用法

基本IO函数的使用(mkstemp)

mkstemp(建立唯一的临时文件)
表头文件 #include<stdlib.h>
定义函数 int mkstemp(char * template);
函数说明:
     mkstemp()用来建立唯一的临时文件。参数 template 所指的文件
     名称字符串中最后六个字符必须是 XXXXXX。mkstemp()会以可
     读写模式和 0600 权限来打开该文件,如果该文件不存在则会建立
     该文件。打开该文件后其文件描述词会返回。
     文件顺利打开后返回可读写的文件描述词。若果文件打开失败则返
     回 NULL,并把错误代码存在 errno 中。

错误代码
     EINVAL 参数 template 字符串最后六个字符非 XXXXXX。
     EEXIST 无法建立临时文件。
    
附加说明
    参数 template 所指的文件名称字符串必须声明为数组,如:
        char template[ ] =”template-XXXXXX”;
         千万不可以使用下列的表达方式
        char *template = “template-XXXXXX”;

1 #include <stdlib.h>
2 #include <stdio.h>
3
4  int main(void)
5 {
6 char temp[] = "tempfile_XXXXXX";
7 char content[] = "This is written by the program.";
8 int fd;
9 fd = mkstemp( temp );
10 write( fd, content, sizeof(content) );
11 printf( "The template file name is %s\n", temp );
12 close( fd );
13 return 0;
14 }
15

原文地址:https://www.cnblogs.com/BloodAndBone/p/1938022.html