|
|
|
|
 如何产生临时文件名? - hcb [ 2005-06-28 12:11 | 761 byte(s)]
 Re: 如何产生临时文件名? - z_york [ 2005-06-29 09:49 | 233 byte(s)]
 Re: 如何产生临时文件名? - fishyum [ 2005-06-29 00:51 | 339 byte(s)]
 Re: 如何产生临时文件名? - passworld [ 2005-06-28 12:36 | 468 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
Red Hat Linux 9
我想在自己的的临时文件目录(如$HOME/tmp)下,产生一个临时文件名,然后由用户程序创建打开读写关闭等正常的操作,不再需要的时候,由用户的程序执行删除操作,该用什么函数来产生呢?
以前在其他系统中用tempnam(),但是在RH9中,编译不过,要我用mkstemp()来代替,该函数好象是返回文件描述符,关闭后自动删除,不符合我的要求
mktemp()函数编译也通不过
#include <stdio.h>
int main()
{
char *temp;
temp=mktemp("/tmp/tmpfileXXXXXX");
exit(0);
}
gcc -o test test.c
显示警告
然后执行
./test
显示段错误
该用什么函数才能正确呢?
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
看说明仔细点,有什么警告把内容贴出来,你以为别人都是神仙?用 tempnam().
The mktemp() function generates a unique temporary file name from tem-
plate. The last six characters of template must be XXXXXX and these are
replaced with a string that makes the filename unique. Since it will be
modified, template must not be a string constant, but should be declared as a
character array.
|
|
|
----
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
我也用着不是很爽 tempnam好象是deprecated,忘记哪个函数了好象也是coredump了。
不知道FILE *tmpfile()函数能不能满足你的要求,这个函数产生一个文件指针,你不知道其文件名是什么,也不知道在哪里。但是这个文件指针是可用来读和写的,当你fclose它的时候就删除了。我曾试图找一找看它到底生成了一个什么文件名的文件,可是一直都没有找到。
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
你应该这样:
char tmpname[] = "/tmp/tmpfileXXXXXX";
char * name = mktemp(tmpname);
mktemp的手册说:你不可把一个constant string传入mktemp,要传一个char数组。
|
|
|
----
I love David Beckham and Man.Utd. for ever.
|
|
[Original]
[Print]
[Top]
|
|
|