zoukankan      html  css  js  c++  java
  • 创建临时文件和目录

    前言: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
    ",line);
        printf("%s
    ",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
    ";
        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
    ",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
    ");
        }
        
        else if(S_ISREG(fileinfo.st_mode)) //判断是否是普通文件
        {
            printf("the file is regular file
    ");
        } 
        printf("temp directpry name:%s
    ",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
    ",buf5); 
        
        while(1)
        {
           ep = readdir(entry); //读目录中的文件
           if(ep == NULL)
               break;
               printf("%s
    ",ep->d_name); //打印目录中的文件名字
        } 
        unlink(path); //解除连接,从文件系统中删除创建的临时目录
        return 0; 
        
    }

    运行效果如下:

  • 相关阅读:
    Java实现 LeetCode 467 环绕字符串中唯一的子字符串
    Java实现 LeetCode 467 环绕字符串中唯一的子字符串
    从ramdisk根文件系统启动Linux成功
    linux中的设备名称和设备号
    设备与驱动的关系以及设备号、设备文件
    使用BusyBox制作Linux根文件系统
    使用Busybox-1.2.0制作根文件系统
    使用BusyBox制作根文件系统
    使用busybox制作rootfs
    uboot里读sd卡内容
  • 原文地址:https://www.cnblogs.com/wurenzhong/p/7553474.html
Copyright © 2011-2022 走看看