利用mkstemp创建程序退出后不会被删除的临时文件

int mkstemp (char *template)

ENOENT为文件不存在时errno的值。

一、源代码:



  1 #include "apue.h"
  2 #include <sys/errno.h>
  3
  4 void make_tempfile(char *);
  5 int main()
  6 {
  7         char valid_name[] = "/tmp/dirXXXXXX";
  8         char *unvalid_name = "/tmp/dirXXXXXX";
  9         printf("Trying to create the first temp file.\n");
 10         make_tempfile(valid_name);
 11         printf("Trying to create the second temp file.\n");
 12         make_tempfile(unvalid_name);
 13         exit(0);
 14 }
 15
 16 void make_tempfile (char *template)
 17 {
 18 //      FILE *fp;
 19         struct stat buf;
 20         int fd;
 21         if ((fd = mkstemp(template)) < 0)
 22                 err_sys("mkstemp error");
 23         printf("file name:%s.\n",template);
 24         close (fd);
 25         if (stat(template,&buf) < 0){
 26                 if(errno == ENOENT)
 27                         err_sys("file doesn't exist");
 28                 else
 29                         err_sys("stat error");
 30         }
 31         else{
 32                 printf("file exist\n");
 33                 unlink(template);
 34         }
 35 }


二、执行结果:

<bldc:/home/tingbinz/apue.3e/SBSCODE/5>R*_*G:./crt_ext_tmpf
Trying to create the first temp file.
file name:/tmp/dircPaWtk.
file exist
Trying to create the second temp file.
Segmentation Fault(coredump)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值