INUX下建立临时的方法(函数)有很多, mktemp, tmpfile等等. 今天只推荐最安全最好用的一种: mkstemp.

mkstemp (建立唯一临时文件)
头文件: #include <stdlib.h>
声明:   int mkstemp(char *template)
返回值: 成功则返回0, 失败则返回-1.
说明:  建立唯一临时文件名, template须以数组形式声明而非指针形式. 
        template格式为: template.XXXXXX. 最后6位必须为XXXXXX, 前缀随意.

//1.mkstemp函数在系统中以唯一的文件名创建一个文件并打开,而且只有当前用户才能访问这个临时文件,
//2.mkstemp函数只有一个参数,这个参数是个以“XXXXXX”结尾的非空字符串。mkstemp函数会用随机产生的字符串替换“XXXXXX”,保证 了文件名的唯一性
//3.由于mkstemp函数创建的临时文件不能自动删除,所以执行完 mkstemp函数后要调用unlink函数,unlink函数删除文件的目录入口

char temp_template[] = "/tmp/htp.XXXXXX";

tfd = mkstemp(temp_template);
if(!(tfp = fdopen(tfd,"w"))) {
fprintf(stderr,"Could not open temp file.\n");
exit(1);
}

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int fd;
char temp_file[]="tmp_XXXXXX";
/*Creat a temp file.*/
if((fd=mkstemp(temp_file))==-1)
{
printf("Creat temp file faile./n");
exit(1);
}
/*Unlink the temp file.*/
unlink(temp_file);
/*Then you can read or write the temp file.*/
//ADD YOUR CODE;
/*Close temp file, when exit this program, the temp file will be removed.*/
close(fd);
}

产生唯一的临时文件mkstemp()的更多相关文章

  1. pytest之将多个测试用例放在一个类中,生成唯一临时文件夹

    将多个测试用例放在一个类中 简单来说就是将多个测试用例放到类中,通过pytest去管理,这和Testng很像.示例代码如下: """ 将多个测试用例放到一个类中执行 &q ...

  2. 文件操作篇 close creat dup dup2 fcntl flock fsync lseek mkstemp open read sync write

    文件操作篇 close creat dup dup2 fcntl flock fsync lseek mkstemp open read sync write close(关闭文件) 相关函数 ope ...

  3. 自学Linux Shell14.3-创建临时文件

    点击返回 自学Linux命令行与Shell脚本之路 14.3-创建临时文件mktemp Linux系统保留了一个特殊的目录位置,以供临时文件使用.Linux使用/tmp目录处理不需要永久保存的文件. ...

  4. linux之创建临时文件的方法

    有时候,我们需要创建文件临时存放一些输出的信息,创建文件时就可能出现文件名存在的问题.如何创建唯一的文件名,Linux为我们提供几个方案: 1.mktemp(强烈推荐) The  mktemp  ut ...

  5. sys/types.h fcntl.h unistd.h sys/stat.h

    sys/types.h 是Unix/Linux系统的基本系统数据类型的头文件,含有size_t,time_t,pid_t等类型. 在应用程序源文件中包含 <sys/types.h> 以访问 ...

  6. Unix/Linux环境C编程入门教程(40) 初识文件操作

     1.函数介绍 close(关闭文件) 相关函数 open,fcntl,shutdown,unlink,fclose 表头文件 #include<unistd.h> 定义函数 int ...

  7. linux网络编程--Circular Buffer(Ring Buffer) 环形缓冲区的设计与实现【转】

    转自:https://blog.csdn.net/yusiguyuan/article/details/18368095 1. 应用场景 网络编程中有这样一种场景:需要应用程序代码一边从TCP/IP协 ...

  8. 【Linux_Unix系统编程】chapter5 深入探究文件IO

    Chapter5 深入探究文件I/O 本章节将介绍另一个与文件操作相关的系统调用:多用途的fcntl(),并展示其应用之一读取和设置打开文件的状态标志. 5.1 原子操作和竞争条件 所有系统调用都是以 ...

  9. [非常重要的总结] Linux C相关函数

    (1)字符测试函数 isalnum(测试字符是否为英文字母或数字) isalpha(测试字符是否为英文字母) isascii(测试字符是否为ASCII码字符) isblank(测试字符是否为空格字符) ...

随机推荐

  1. 在虚拟机上安装linux系统

    1.安装linux服务器,内存4G,默认典型,next安装程序光盘影像文件,next选版本 2.6.x内核64位,next选择虚拟机位置(至少10G),next最大磁盘20G,选择单文件,next自定 ...

  2. Java判断一个字符串str不为空:方法及时间效率

    判断一个字符串str不为空的方法有: 1.str == null; 2.”“.equals(str): 3.str.length <= 0; 4.str.isEmpty(): 注意:length ...

  3. Function.caller, arguments.caller, arguments.callee, arguments.callee.calller

    Function.caller指向当前函数的调用者,是arguments.caller的替代者 arguments.caller也是指向当前函数的调用者,已被废弃 arguments.callee是对 ...

  4. 解题:APIO 2014 回文串

    题面 初见SAM 洛谷数据太弱了,我SAM写错了居然有90pts=.=??? SAM求一个子串$(l,r)$的出现次数:从右端点对应状态开始在parent树上倍增,当目标节点的$len$大于等于子串长 ...

  5. 【loj6198】谢特

    Portal -->loj6198 Solution ​ (为什么感觉loj上面这几道后缀数组的题..套路都是一样的啊qwq) ​ 同样也是..考虑某个区间\(height[i]\)的最小值的贡 ...

  6. web项目中解决post乱码和get乱码的方法

    前提复习编码问题产生的原因: 1.  什么是URL编码. URL编码是一种浏览器用来打包表单输入的格式,浏览器从表单中获取所有的name和其对应的value,将他们以name/value编码方式作为U ...

  7. golang设置代理

    http://note.youdao.com/noteshare?id=a8df0ec2d623f282a782dbe937bdae9f

  8. 「Django」rest_framework学习系列-权限认证

    权限认证:1.项目下utils文件写permissions.py文件 from rest_framework.permissions import BasePermission class SVIPP ...

  9. LeakCanary原理分析

    参考文档 http://blog.csdn.net/wyfei021/article/details/46506521http://vjson.com/wordpress/leakcanary%e6% ...

  10. background(css复合写法)

    1. 背景-background========================================================== 单个属性的写法 .sample1 { /*背景颜色 ...