Chinaunix首页 | 论坛 | 博客
  • 博客访问: 343726
  • 博文数量: 60
  • 博客积分: 1570
  • 博客等级: 上尉
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-02 23:37
文章分类

全部博文(60)

文章存档

2012年(2)

2010年(2)

2009年(56)

分类: LINUX

2009-11-30 18:12:34

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。

 

阅读(5255) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~