#include <stdio.h>
char *tmpnam(char *s);
#include <stdio.h>
FILE *tmpfile(void);
#include <stdio.h>
#include <stdlib.h>
int main()
{
char tmpname[L_tmpnam];
char *filename;
FILE *tmpfp;
filename = tmpnam(tmpname);
printf("Temporary file name is: %s\n", filename);
tmpfp = tmpfile();
if(tmpfp)
printf("Opened a temporary file OK\n");
else
perror("tmpfile");
exit(0);
}
[root@localhost C]# gcc -g tmpnam.c -o tmpnam.exe
/tmp/ccvqY2y6.o: In function `main':
/root/C/tmpnam.c:10: warning: the use of `tmpnam' is dangerous, better use `mkstemp'
[root@localhost C]# ./tmpnam.exe
Temporary file name is: /tmp/fileDI1Csz
Opened a temporary file OK
[root@localhost C]# cat tmpnam.c
#include <stdlib.h>
char *mktemp(char *template);
int mkstemp(char *template);
01 #include <stdio.h>
02 #include <stdlib.h>
03 #include <fcntl.h>
04
05 int main()
06 {
07 char *s;
08 s = tmpnam(NULL);
09 printf("%s\\n", s);
10 open( s, O_CREAT );
11
12 return 0;
13 }
运行及输出结果(通过产生的文件名创建文件):
[root@localhost C]# ./tmpnam-2.exe
/tmp/filewifJJK
[root@localhost C]# ll /tmp/filewifJJK
-r-Sr-s--- 1 root root 0 Feb 21 17:29 /tmp/filewifJJK
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |