zoukankan      html  css  js  c++  java
  • c语言系统函数——目录操作

    一、 文件夹的系统函数
        1.mkdir(),创建一个目录文件。
        int mkdir(const char *pathname, mode_t mode);
        第一个参数是路径,第二个参数是权限,一般为0777.
        在shell命令中,使用mkdir可以创建一个目录,如果使用mkdir aa/bb/cc创建目录,而aa不存在时,可以使用mkdir aa/bb/cc -p;
        如果使用mkdir函数来创建,需要一个一个的创建。
        
        2.access()检查是否具有某些权限,是否存在,是否有读写运行的权限等。
         int access(const char *pathname, int mode);
        返回:如果具有这个权限返回0,没有或者异常返回-1.
        
        3.opendir(),打开一个目录文件
           DIR *opendir(const char *name);
           DIR *fdopendir(int fd);
          函数返回一个DIR的指针。
          
        4.readdir();读取目录下面的文件
          struct dirent *readdir(DIR *dirp);
         返回的结果是一个结构体。
          struct dirent {
                   ino_t          d_ino;       /* inode 编号 */
                   off_t          d_off;       /* offset to the next dirent */
                   unsigned short d_reclen;    /* length of this record */
                   unsigned char  d_type;      /* type of file; not supported
                                                  by all file system types */
                   char           d_name[256]; /* 文件名称*/
               };
          其中文件的类型有:1.DT_REG普通文件      2.DT_LIK符号链接      3.DT_DIR目录文件     4.DT_FIFO管道文件
          如果返回NULL说明遍历完了。
          
      例题:

        1.使用系统调用完成mkdir实现建立xx/yy/zz

        2.完成命令ls

        3.完成文件夹的复制

    一 、mkdir的实现。
    
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    //仿写mkdir命令
    //输入的如果是x/y/z;而x并不存在,就会一个一个的创建
    //核心是分割字符串
    
    int main(int argc,char *argv[])
    {
        argv[1];
        //对argv[1]进行分割
        char * p =  strtok(argv[1],"/");
        int erro = 0;
        char * path = (char *)malloc(256);
        strcpy(path,p);
        printf("%s
    ",path);
        if(access(path,F_OK) != 0)
        {
            erro = mkdir(path ,0777);
            if(erro == -1)
            {
                perror("mkdir");
                return -1;
            }
        }
        while(1)
        {
            p = strtok(NULL,"/");
            if(p == NULL)
            {
                break ;
            }
            sprintf(path,"%s/%s",path,p);
            printf("%s
    ",path);
            if(access(path,F_OK) == 0)
            {
                //存在这个目录,不需要创建。
            }
            else
            {
                erro = mkdir(path ,0777);
                if(erro == -1)
                {
                    perror("mkdir");
                    return -1;
                }
            }
        }
        return 0;
    }
    
    
    ls的实现
    
    #include <sys/types.h>
    #include <dirent.h>
    #include <stdio.h>
    
    
    int main(int argc,char *argv[])
    {
        DIR *dir = opendir(argv[1]);
        if(NULL == dir)
        {
            perror("opendir");
            return ;
        }
        //使用路径打开文件。
    
        while(1)
        {
             struct dirent * str_dir = readdir(dir);
             if(NULL == str_dir)
             {
                 break;
             }
             if(strcmp(".",str_dir->d_name) == 0 || 0 == strcmp("..",str_dir->d_name))
             {
                 break;
             }
             printf("%s
    ",str_dir->d_name);
         }
            
        
        return 0;
    }
    
    
    拷贝目录
    
    #include <stdio.h>
    #include <sys/types.h>
    #include <dirent.h>//opendir的头文件 //readdir的头文件
    #include <sys/stat.h>
     #include <fcntl.h>
     //open需要的头文件
    #include <string.h>
    #include <errno.h>      //errno的头文件
    #include <stdlib.h>  
    #include <unistd.h>    //close的头文件
    
    int main(int argc,char *argv[])
    {
        //打印一个文件夹下的各种文件。
        /*
        DIR * folder = opendir(argv[1]);
        if(NULL == folder)
        {
            perror("open");
            return -1;
        }
        
        while(1)
        {
            struct dirent * dir = readdir(folder);
            if(NULL == dir)
            {
                break;
            }
            printf("%s
    ",dir->d_name);
        }
        */
        if(access(argv[2],F_OK) == -1)
        {
            mkdir(argv[2],0777);
        }
        //如果文件不存在,就创建一个文件。
        swap(argv[1],argv[2]);
        return 0;
    }
    
    int swap(char * source,char *destination)
    {
        DIR * sourDir = opendir(source);
        if(source == NULL)
        {
            perror("openDir");
            return -1;
        }
        //遍历目录,如果发现是普通文件就直接复制,目录文件就递归调用
        while(1)
        {
            struct dirent *dir = readdir(sourDir);
            if(NULL == dir)
            {
                break;
            }
            
        
            /*
            if( 0 == strncmp(dir->d_name,".",1))
            {
                continue;
            }
            */
            if(DT_DIR == dir->d_type)
            {
                //去除..和.
                if(strcmp(dir->d_name,"..") == 0 || strcmp(dir->d_name,".") == 0)
                {
                    //printf("%s
    
    ",dir->d_name);
                    continue;
                }
                char sourceDir[256] = {''};
                char destinationDir[256] = {''};
                sprintf(sourceDir,"%s/%s",source,dir->d_name);
                sprintf(destinationDir,"%s/%s",destination,dir->d_name);
                if(access(destinationDir,F_OK) == -1)
                {
                    int ret = mkdir(destinationDir,0777);
                    if(ret == -1)
                    {
                        printf("出错%s
    ",destinationDir);
                        perror("mkdir");
                        return -1;
                    }
                }    
                swap(sourceDir,destinationDir);
            }
            else
            {
                //需要使用一个copy的命令命令
                char sourceDir[256] = {''};
                char destinationDir[256] = {''};
                sprintf(sourceDir,"%s/%s",source,dir->d_name);
                sprintf(destinationDir,"%s/%s",destination,dir->d_name);
                printf("%s
    ",sourceDir);
                printf("%s
    ",destinationDir);
                copy(sourceDir,destinationDir);
                //复制文件。
            }
        }
        return 1;
    }
    
    //拷贝一个文件,如果成功就会返回1,否则就会返回-1.
    int copy(char * p1 ,char * p2)
    {
        int fd = -1 ;
        int fd2 = -1;
        fd = open(p1,O_RDONLY);
        if(-1 == fd)
        {
            perror("open1");
            //exit(EXIT_FAILURE);
            return -1;
        }
        fd2 = open(p2,O_WRONLY|O_CREAT,0664);
        if(-1 == fd2)
        {
            perror("open2");
            //exit(EXIT_FAILURE);
            return -1;
        }
        while(1)
        {
            char x[256];
            ssize_t ret =-1;
            ret = read(fd,x,256);
            if(ret == -1)
            {
                perror("read");
                return -1;
            }
            if(ret == 0)
            {
                break;
            }
    
            ret = write(fd2,x,ret);
            if(ret == -1)
            {
                perror("write");
                return -1;
            }
        }
        close(fd);
        close(fd2);
        return 1;
    }
  • 相关阅读:
    selenium2截图ScreenShot的使用
    selenium2断言类Assert的使用
    selenium2中的TestNg注解和数据驱动的简介及使用
    bash函数定义/使用/传参…
    bash字符串操作
    bash数组操作-定义/初始化/赋值…
    bash实例-参数/函数/统计IP
    01.AutoMapper 之约定(Conventions)
    00.AutoMapper 之入门指南(Getting Started Guide)
    AutoMapper
  • 原文地址:https://www.cnblogs.com/qiny1012/p/8405394.html
Copyright © 2011-2022 走看看