免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3764 | 回复: 0
打印 上一主题 下一主题

创建临时文件:tmpfile 和 mkstemp函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-30 18:12 |只看该作者 |倒序浏览

1.  

#include
FILE *tmpfile(void);
                         Returns: file pointer if OK, NULL on error
函数说明:(返回的是文件指针
The  tmpfile()  function  opens a unique temporary file in binary read/write (w+b) mode.  The file will be automatically deleted when it is  closed  or  the  program terminates.
2.
#include stdlib.h>
int mkstemp(char *template);
                         Returns: file descriptor if OK, 1 on error
函数说明:(返回的是临时文件的打开文件描述符
The  mkstemp()  function generates a unique temporary filename from template.  The last six characters of template must be XXXXXX  and  these  are  replaced  with  a string  that  makes  the  filename  unique.   The  file  is then created with mode read/write and permissions 0666 (glibc 2.0.6 and earlier), 0600 (glibc  2.0.7  and later).   Since  it  will be modified, template must not be a string constant, but should be declared as a character array.  The file  is  opened  with  the  open(2) O_EXCL flag, guaranteeing that when mkstemp() returns successfully we are the only user.
3. 与 tempfile 不同的是,mkstemp 创建的临时文件不会自动被删除。如若想从文件系统名字空间中删除该文件,则需要自行 unlink 它。可以在 mkstemp 执行成功后,立即调用unlink函数。 另一个不同的地方是:mkstemp 指定了创建文件的路径。
4. 还有两个创建临时文件的函数:tmpnam 和 tempnam。
它们的不足的地方是:在返回唯一路径名和应用程序用该路径名创建文件之间有一个时间窗口。在该时间窗口期间,另一个进程可能创建了一个同名文件。
tmpfile 和 mkstemp 函数则不会产生此种问题,可以使用他们代替tmpnam 和 tempnam。



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/105349/showart_2107596.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP