Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5744214
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: C/C++

2007-11-24 15:46:15

    偶尔我们会在我们的程序中使用临时文件,来存放一些临时数据或者是通过临时文件来跟其他的进程进行交互。*nix给我们提供了一些临时文件相关的函数:

    tmpfile (1)         - create a temporary file in a safe manner
    tempnam (3)          - create a name for a temporary file
    tmpnam (3)           - create a name for a temporary file
    mkdtemp (3)          - create a unique temporary directory
    mkstemp (3)          - create a unique temporary file
    mktemp (3)           - make a unique temporary filename

这些函数的使用都是比较简单的,这里我只简单的介绍一些mkstemp函数和tmpfile函数的使用。
mkstemp函数会根据一个模板,生成一个临时的文件,并返回文件描述符。
tmpfile函数会创建一个临时的二进制文件,返回文件指针,如果fclose(fp)或者是程序结束,都会自动删除文件。

原理:
tmpfile函数先调用tmpnam产生一个唯一的路径名,然后,用该路径创建一个二进制文件,并且立即unlink它。
注:unlink一个文件,只是会减少文件的链接计数,只有当链接计数为0,文件才可能被删除。另外,如果一个进程打开这个文件的话,也是不可以删除的。关闭一个文件,内核首先检查打开文件的进程数目,如果进程数为0,然后检查链接计数,如果也为0,那么就可以删除文件了。
mkstemp创建的临时文件不会被自动删除,如果需要,需要手动的unlink它。

tmpnam和tempnam有一个不足的地方就是存在时间窗口的竞争问题,他们首先生成临时文件的名字,再创建临时文件,这样的话,就会导致可能两个进程生成相同的名字,从而创建相同文件名的临时文件。
tmpfile和mkstemp函数没有上面的问题,tmpfile又由于生成的是一个二进制文件,所以这里比较推荐使用mkstemp函数来创建临时文件,不过最后需要unlink。

Advanced Linux Programming中的一段例子代码:
#include
#include
#include
#include

/* A handle for a temporary file created with write_temp_file.  In
   this implementation, it's just a file descriptor.  */
typedef int temp_file_handle;

/* Writes LENGTH bytes from BUFFER into a temporary file.  The
   temporary file is immediately unlinked.  Returns a handle to the
   temporary file.  */

temp_file_handle write_temp_file (char* buffer, size_t length)
{
  /* Create the filename and file.  The XXXXXX will be replaced with
     characters that make the filename unique.  */
  char temp_filename[] = "/tmp/temp_file.XXXXXX";
  int fd = mkstemp (temp_filename);
  /* Unlink the file immediately, so that it will be removed when the
     file descriptor is closed.  */
  unlink (temp_filename);
  /* Write the number of bytes to the file first.  */
  write (fd, &length, sizeof (length));
  /* Now write the data itself.  */
  write (fd, buffer, length);
  /* Use the file descriptor as the handle for the temporary file.  */
  return fd;
}

/* Reads the contents of a temporary file TEMP_FILE created with
   write_temp_file.  The return value is a newly-allocated buffer of
   those contents, which the caller must deallocate with free.
   *LENGTH is set to the size of the contents, in bytes.  The
   temporary file is removed.  */

char* read_temp_file (temp_file_handle temp_file, size_t* length)
{
  char* buffer;
  /* The TEMP_FILE handle is a file descriptor to the temporary file.  */
  int fd = temp_file;
  /* Rewind to the beginning of the file.  */
  lseek (fd, 0, SEEK_SET);
  /* Read the size of the data in the temporary file.  */
  read (fd, length, sizeof (*length));
  /* Allocate a buffer and read the data.  */
  buffer = (char*) malloc (*length);
  read (fd, buffer, *length);
  /* Close the file descriptor, which will cause the temporary file to
     go away.  */
  close (fd);
  return buffer;
}

int main()
{
        temp_file_handle temp;
        int len;
        char buff[]="hello,world!";
        char *ret;

        printf("Create TempFile:\n");
        temp = write_temp_file (buff, strlen(buff));
        ret = read_temp_file(temp,&len);

        *(ret+len)='\0';
        printf("Read TempFile:\n%s\n",ret);

        return 0;
}

运行结果:
wangyao@fisherman:~/Desktop/Advanced Linux Programming/train/tempfile$ ./tempfile
Create TempFile:
Read TempFile:
hello,world!
wangyao@fisherman:~/Desktop/Advanced Linux Programming/train/tempfile$

阅读(3091) | 评论(0) | 转发(0) |
0

上一篇:getopt函数的使用

下一篇:进程的相关概念

给主人留下些什么吧!~~