创建临时文件和目录

前言:unix下除了有实实在在的文件外,还可以创建临时的文件和目录,这里介绍两个创建临时文件的函数,tmpfilef和mkstemp,以及创建临时目录的函数mkdtemp。这三个函数具体用法如下。

 

一、创建一个无名的临时文件,程序退出时关闭临时文件
(1)头文件        #include <stdio.h>
(2)函数原型   FILE *tmpfile(void);
(3)返回值
            成功:返回一个文件流
            失败:NULL,没有创建临时文件
注意:tmpfile创建的临时文件只能读取一次,读取之后里面的内容就没有了

具体代码如下:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>


#define MAXLINE  1029   

int main(int argc,char *argv[])
{
    char tempname[100] = "hello";
    char line[MAXLINE];  //L_tmpnam是定义在头文件<stdio.h>中
    FILE *fp;
    char buf[100];
    fp = tmpfile();  //创建临时文件
    fputs("one line output",fp);  //把数据写到临时文件流中
    rewind(fp); //重置流到文件头
    fgets(line,sizeof(line),fp);  //从临时文件流中读取数据
    fgets(buf,sizeof(buf),fp);  //tmpfile创建的临时文件只能读取一次,读取之后里面的内容就没有了     
    printf("%s\n",line);
    printf("%s\n",buf);
    
    //fputs(line,stdout);  //输出数据  
    return 0;
  }

 运行结果如下:

从图中的显示结果可知道验证了这句话:tmpfile创建的临时文件只能读取一次,读取之后里面的内容就没有了

 

二、创建一个临时文件,文件名自定义
(1)函数原型  int mkstemp(char *template);
(2)参数:
            a、template:临时文件的名字,最后6个字符一定是XXXXXX,绝对不能少,记得是大写
(3)返回值:
             成功:文件描述符,并创建一个临时文件,文件名是自定义的,后6个字符是系统自己填充
             失败:-1
特点:这个函数创建的临时文件可以读多次,而且数据不会丢失,这是上面tmpfile函数所没有的功能

具体代码如下:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>


#define MAXLINE  1029

int main(int argc,char *argv[])
{
    int fd;
    char name[] = "/tmp/dirXXXXXX";  //后面记得加XXXXXX,并且是大写的,创建临时文件成功的时候系统会自动填充字符
    char buf2[100],buf1[100] = "hello world\n";
    char buf3[100];
    bzero(buf3,100); 
    bzero(buf2,sizeof(buf2));  
    fd = mkstemp(name);  //创建一个临时文件
    if(fd == -1)
    {
        perror("mkstemp");
        exit(1);
    }
    printf("temp file name is: %s\n",name); 
    write(fd,buf1,strlen(buf1)); //写数据到临时文件中
    lseek(fd,0,SEEK_SET);  //重新把文件指针偏移到文件头
    
    read(fd,buf2,sizeof(buf2));  //从临时文件中读出数据
    lseek(fd,0,SEEK_SET);  //重新把文件指针偏移到文件头
    
    read(fd,buf3,sizeof(buf3));   //可以读多次,而且数据不会丢失
    printf("%s",buf3);
    fputs(buf2,stdout);
return 0;
}

运行效果如下:

比较上面的函数这里的区别出来了:这个函数创建的临时文件可以读多次,而且数据不会丢失,这是上面tmpfile函数所没有的功能

 

 

好了上最后一个函数:

三、创建临时目录
(1) 函数原型    char *mkdtemp(char *template);
(2)参数:临时目录的路径,最后6个字符一定是XXXXXX,绝对不能少,记得是大写
(3)返回值:
              成功:返回指向目录名的指针,创建目录成功
              失败:NULL
注意:这个程序退出后不会自动从文件系统中删除目录,要用unlink函数来删除

代码如下:

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>


#define MAXLINE  1029

int main(int argc,char *argv[])
{
    char *path;
    DIR *entry;
    struct dirent *ep;
    struct stat fileinfo;
    bzero(&fileinfo,sizeof(fileinfo));
    char name[] = "/tmp/dirXXXXXX";
    path = mkdtemp(name);  //创建一个临时目录
    if(path == NULL)
    {
        perror("mkdtemp");
        exit(1);
    }
    
    lstat(path,&fileinfo);
    if(S_ISDIR(fileinfo.st_mode))  //判断是否是目录
    {
        printf("the file is directory\n");
    }
    
    else if(S_ISREG(fileinfo.st_mode)) //判断是否是普通文件
    {
        printf("the file is regular file\n");
    } 
    printf("temp directpry name:%s\n",path);
    
    
    entry = opendir(path);  //打开目录
    chdir(path);  //进入目录
    mkdir("test",0777); //创建一个名字为test的目录
    int fd = open("test.txt",O_RDWR|O_CREAT,0777);  //以读写权限打开一个名字为test.txt的文件,如果不存则创建
     
    //可以在临时目录中创建文件并进行读写操作
    char buf4[] = "hello world";
    char buf5[100];
    bzero(buf5,sizeof(buf5));
    write(fd,buf4,sizeof(buf4));
    lseek(fd,0,SEEK_SET); //重新把文件指针偏移到文件头
    read(fd,buf5,sizeof(buf5));
    printf("%s\n",buf5); 
    
    while(1)
    {
       ep = readdir(entry); //读目录中的文件
       if(ep == NULL)
           break;
           printf("%s\n",ep->d_name); //打印目录中的文件名字
    } 
    unlink(path); //解除连接,从文件系统中删除创建的临时目录
    return 0; 
    
}

运行效果如下:

 

转载于:https://www.cnblogs.com/wurenzhong/p/7553474.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值