热门关键字:  ubuntu  分区  Fedora  linux系统进程  函数

当前位置 :| 主页>Linux教程>编程开发>

在linux系统下使用C语言操作临时文件

来源: 作者: 时间:2007-03-09 Tag: 点击:

有时程序需要存储很大量的数据,或者在几个进程间交换数据,这时您可能考虑到使用临时文件。使用临时文件要考虑几个问题:

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

#include

/* 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. */



相关文章:
ioctl函数
struts与spring三种整合方法
进程的创建fork
jsp速成
Hibernate单向一对多应注意的问题
Java RMI之HelloWorld篇
常用的Eclipse快捷键
sparc linux 系统调用
一个关于jboss "Halting VM"的情况处理
编写更好的Linux程序3(这次只讲void *)
Tikiwiki 2.2+ Ucenter 1.5(Discuz)整合
玩转ptrace(一)
玩转ptrace(二)
dup2
pipe
Jboss的学习
gcc用法
将UI从代码中解放出来
数据结构与算法(2)
有关于JAVA中的CLASSPATH的作用
const成员函数
虚函数
《c和指针》读书笔记2
初次接触Zend Framework需要掌握的内容
php中require和include的简单区别
正则表达式在PHP中的应用
在PHP中使用与Perl兼容的正则表达式
Zend Studio 生成 wsdl
freebsd7 + php5.2.6 + xdebug2.0.2安装
PHP抓取远程网站数据的代码