Linxu的目录操作函数(详解)

chdir函数

//头文件
 #include <unistd.h>
//原型
int chdir(const char *path);
 int fchdir(int fd);
//功能:chdir函数用于改变当前工作目录。
//返回值: 如果返回0, 代表成功。如果返回-1代表读取不成功,错误代码存入errno中
//参数:
//path 改变到的目录

getcwd函数

//头文件
 #include <unistd.h>
//原型
 char *getcwd(char *buf, size_t size);
  char *getwd(char *buf);
  char *get_current_dir_name(void);
//功能:getcwd()会将当前的工作目录绝对路径复制到参数buf 所指的内存空间,参数size 为buf 的空间大小。
//返回值:当前的工作目录绝对路径
//参数:
//1.buf 所指的内存空间
//2.size 为buf 的空间大小。

举例

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

int main(int argc, char* argv[])
{
    if(argc < 2)
    {
        printf("a.out dir\n");
        exit(1);
    }

    int ret = chdir(argv[1]);
    if(ret == -1)
    {
        perror("chdir");
        exit(1);
    }

    int fd = open("chdir.txt", O_CREAT | O_RDWR, 0777);
    if(fd == -1)
    {
        perror("open");
        exit(1);
    }
    close(fd);

    char buf[128];
    getcwd(buf, sizeof(buf));
    printf("current dir: %s\n", buf);

    return 0;
}

mkdir函数

//头文件
 #include <sys/stat.h>
  #include <sys/types.h>
//原型
int mkdir(const char *pathname, mode_t mode);
//功能:创建一个目录
//返回值:返回0 表示成功, 返回 -1表示错误,并且会设置errno值。 
//参数:
//1.pathname路径
//2.mode权限,注意创建目录时必须有可执行权限,否则无法进入目录

rmdir函数

//头文件
#include <unistd.h>
//原型
 int rmdir(const char *pathname);
//功能:删除一个目录
//返回值:返回0 表示成功, 返回 -1表示错误,并且会设置errno值。 
//参数:
//1.pathname路径

opendir函数

//头文件
#include <sys/types.h>
 #include <dirent.h>
//原型
 DIR *opendir(const char *name);
 DIR *fdopendir(int fd);
//功能: 打开一个目录
//返回值:在失败的时候返回一个空的指针,成功时返回DIR*类型的指针指向打开的目录。 
//参数:
//path 路径

readdir函数

//头文件
 #include <dirent.h>
//原型
 struct dirent *readdir(DIR *dirp);
//功能: 读取打开的目录
/*返回值: 
struct dirent {
               ino_t          d_ino;                     //此目录进入点的inode
               off_t          d_off;                       //目录文件开头至此目录进入点的位移
               unsigned short d_reclen;    //d_name 长度,不含NULL字符
               unsigned char  d_type;       //d_name所指文件类型
               char           d_name[256];      //文件名
               };
      d_type类型包括:
       		  DT_BLK   -- 块设备
              DT_CHR   --字符设备  
              DT_DIR     -- 目录
              DT_FIFO   --  软连接
              DT_LNK    --  管道
              DT_REG    -- 普通文件
              DT_SOCK  --套接字   
              DT_UNKNOWN --未知
 */
//参数:
//dirp 打开的目录指针

closedir函数

//头文件
#include <sys/types.h>
#include <dirent.h>
//原型
 int closedir(DIR *dirp);
//功能: 关闭一个目录
//返回值:返回0 表示成功, 返回 -1表示错误。 
//参数:
///dirp 打开的目录指针

举例:递归读取目录获取文件个数

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int getFileNum(char* root)
{
    // 打开目录
    DIR* dir = opendir(root);
    if(dir == NULL)
    {
        perror("opendir");
        exit(0);
    }

    // 读目录
    int total = 0;
    char path[1024] = {0};
    struct dirent* ptr = NULL;
    while((ptr = readdir(dir)) != NULL)
    {
        // 跳过 . 和 ..
        if(strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
        {
            continue;
        }
        // 判断是不是文件
        if(ptr->d_type == DT_REG)
        {
            total ++;
        }
        // 如果是目录
        if(ptr->d_type == DT_DIR)
        {
            // 递归读目录
            sprintf(path, "%s/%s", root, ptr->d_name);
            total += getFileNum(path);
        }
    }
    closedir(dir);
    return total;
}

int main(int argc, char* argv[])
{
    // 读目录, 统计文件个数
    int total = getFileNum(argv[1]);
    // 打印
    printf("%s has file number: %d\n", argv[1], total);
    return 0;
}
发布了37 篇原创文章 · 获赞 36 · 访问量 3588

猜你喜欢

转载自blog.csdn.net/qq_43799957/article/details/105190808
今日推荐