在linux下使用c语言操作临时文件

2016-02-19 20:50 6 1 收藏

下面这个在linux下使用c语言操作临时文件教程由图老师小编精心推荐选出,过程简单易学超容易上手,喜欢就要赶紧get起来哦!

【 tulaoshi.com - 编程语言 】

/*******************************************************************本文首发于bbs.bluegem.org的CWorld区*本人email:chenfei@sohu.com*如转载本文,请保留首发地和本人联络方式,以方便交流, !-- frame contents -- !-- /frame contents -- 谢谢!******************************************************************/     有时程序需要存储很大量的数据,或者在几个进程间交换数据,这时您可能考虑到使用临时文件。使用临时文件要考虑几个问题:
  1、保证临时文件间的文件名不互助冲突。
  2、保证临时文件中内容不被其他用户或者黑客偷看、删除和修改。
  所以在Linux下有专门处理临时文件的函数
  mkstemp函数
      mkstemp函数将在系统中以独一无二的文件名创建一个文件并打开,而且只有当前用户才有访问这个临时文件的权限,当前用户对这个临时文件可以打开并进行读、写操作。mkstemp函数只有一个参数,这个参数是个以“XXXXXX”结尾的非空字符串。mkstemp函数会用随机产生的字符串替换“XXXXXX”,保证了文件名的唯一性。函数返回一个文件描述符,假如执行失败返回-1。在glibc 2.0.6 以及更早的glibc库中这个文件的访问权限是0666 ,glibc 2.0.7 以后的库这个文件的访问权限是0600。
      当临时文件完成她的使命假如不把它清除干净把或者程序由于意外在临时文件被清除前就已经退出,临时文件所在的目录会塞满垃圾。由于mkstemp函数创建的临时文件不能自动删除(请参考下文中的tmpfile函数)。执行完mkstemp函数后要调用unlink函数,unlink函数删除文件的目录入口,所以临时文件还可以通过文件描述符进行访问,直到最后一个打开的进程关闭文件操作符,或者程序退出后临时文件被自动彻底地删除。
  例程:
  直接使用advanced linux programming的例程,只把注释翻译一下#include stdlib.h
  #include unistd.h
  /* A handle for a temporary file created with write_temp_file. In
  this implementation, it’s just a file descriptor. */
  /*write_temp_file是个操作临时文件的句柄,本例中只是个文件描述符*/
  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. */
  /*在这函数从BUFFER中向临时文件写入LENGTH字节数据。临时文件在刚一创建就被删除掉。函数会返回临时文件的句柄。*/
  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. */
  /*新建文件名和文件,文件名中的XXXXXX将被随机字符串代替,以保证文件名在系统中的唯一性*/
   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,这样只要文件描述符一关闭文件就会被自动删除*/
   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. */
  /*从被write_temp_file创建的临时文件中读取数据。返回值是含有文件内容的新申请到的内存块,这块内存应该又调用read_temp_file者释放。
  *length是临时文件正文内容的长度。执行完read_temp_file函数后临时文件被彻底删除*/
  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. */
   /*fd是访问临时文件的文件描述符*/
   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;
  }
  
  tmpfile函数
      假如您使用C library I/O函数,并且并没有另一个程序使用这个临时文件(笔者注:按我的理解是在同一进程或具有父子关系的进程组中),有个更简洁的函数——tmpfile。tmpfile函数创建并打开一个临时文件,并且自动执行了unlink了这个临时文件。tmpfile函数返回一个文件描述符,假如执行失败返回NULL。当程序执行了fclose或者退出时,资源被释放。
      linux系统中还提供mktemp、 tmpnam、 和tempnam等函数,但是由于健壮性和安全方面理由不建议使用他们。 更多内容请看C/C++进阶技术文档  C++编程  Linux文件相关文章专题,或

来源:https://www.tulaoshi.com/n/20160219/1624653.html

延伸阅读
标签: MySQL mysql数据库
  受影响系统: MySQL AB MySQL 4.1.0-alpha MySQL AB MySQL 4.1.0 MySQL AB MySQL 4.0.9 MySQL AB MySQL 4.0.8 MySQL AB MySQL 4.0.7 MySQL AB MySQL 4.0.6 MySQL AB MySQL 4.0.5a MySQL AB MySQL 4.0.5 MySQL AB MySQL 4.0.4 MySQL AB MySQL 4.0.3 MySQL AB MySQL 4.0.2 MySQL AB MySQL 4.0.15 MySQL AB MySQL 4.0.14 MySQL AB My...
标签: windows
Win7系统临时文件怎么转移?我们在下载软件的时候,系统盘的位置肯定不会去占用的,都会手动去修改路径,但是,慢慢的你会发现,系统盘的空间还是越来越小,究其原因,临时文件夹是放在系统盘中的。现在我们为了系统的更好的运行,要把临时文件夹换到其他地方,下面小编就告诉大家具体的Win7系统临时文件转移操作步骤。 1.首先打开W...
标签: 电脑入门
现在很多人在给电脑安装软件时,都已经养成了修改安装路径的习惯,将软件安装到其它硬盘分区,可以为系统盘节省不少空间。可是电脑用久了,系统盘的占用率还是会慢慢增长,到底是什么文件占用了系统盘空间呢?事实上,在Win7系统中,临时文件是个吃硬盘的大户,而且是在不知不觉中慢慢吞噬我们的硬盘空间。同时,临时mirclient.dll文件也是Win7...
标签: windows10
Win10怎么删除系统临时文件?   1、单击开始菜单,点击设置。 2、打开设置后,依次打开系统储存。 3、选择你要清理的磁盘,一般我们选择系统分区C盘。 4、选择后,系统人列出储存使用情况,我们选点击下方的临时文件。 5、然后点击删除临时文件按钮就可以了。 Win10系统没有睡眠功能怎么办?   ...
标签: 电脑入门
下面我们以如今流行的WIN7系统为例,介绍下如何将临时文件夹从系统盘中转移出去。Windows XP系统方法也类似,不过XP系统已经成为过去式,有兴趣的朋友也可以去试试,方法如下: ⒈)首先进入WIN7系统的控制面板--之后再进入系统与安全然后在界面的左上角会看到有高级系统设置我们点击进入即可,如下图: WIN7高级系统设置 ⒉)点击上图中...

经验教程

447

收藏

79
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部