Linux系统编程—目录

1. 创建目录

#include <sys/stat.h>
#include <sys/types.h>

int mkdir(const char *pathname, mode_t mode);
  • 创建路径为 pathname 的目录,其访问权限为 mode;注意,mkdir() 创建的只是路径名中的最后一部分,它并不会自动创建缺失的父目录。
  • 成功时返回 0,失败时返回 -1。
  • 如果指定的目录已经存在,则失败返回,并将 errno 设为 EEXIST。

2. 删除目录

#include <unistd.h>

int rmdir(const char *pathname);
  • 删除指定的空目录,该目录必须为空。
  • 成功时返回 0,失败时返回 -1。

3. 读取目录

#include <sys/types.h>
#include <dirent.h>

DIR *opendir(const char *name);
  • 打开指定的目录。
  • 成功时返回指向该目录的句柄,失败时返回 NULL
#include <dirent.h>

struct dirent *readdir(DIR *dirp);
  • 从一个目录流中读取连续的条目。
  • 成功时,返回一个指向静态分配的数据结构的指针;到达流的尾部或出错时,返回 NULL
  • readdir() 会返回 ...
struct dirent {
    ino_t          d_ino;       /* Inode number */
    ...
    char           d_name[256]; /* Null-terminated filename */
};
#include <sys/types.h>
#include <dirent.h>

void rewinddir(DIR *dirp);
  • 回到目录流的起点。
#include <sys/types.h>
#include <dirent.h>

int closedir(DIR *dirp);
  • 关闭目录流。
  • 成功时返回 0,失败时返回 -1。

4. 遍历文件树

#include <ftw.h>

int nftw(const char *dirpath,
        int (*fn) (const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf),
        int nopenfd, int flags);
  • 递归遍历指定的目录,并对其中的每个文件执行指定的操作。

  • 默认情况下,ntfw() 会针对给定的目录树执行未排序的前序遍历,即先处理目录,再处理目录下的文件和子目录。

  • ntfw() 在遍历目录树时,最多会为树的每一层打开一个文件描述符;nopenfd 指定了 ntfw() 最多可用的描述符数;如果目录树的深度超过了这一个值,那么 ntfw() 会在做好记录的前提下,关闭并重新打开描述符。

  • 通过 flags 参数可以自定义 ntfw() 的某些行为,可取值如下(可位或指定多个):

    • FTW_CHDIR:在处理目录的内容之前先调用 chdir() 进入该目录。
    • FTW_DEPTH:对目录树执行后序遍历,即先处理目录下的所有文件和子目录,再处理目录本身。
    • FTW_MOUNT:不会越界进入另一个文件系统;因此,如果目录树中的某一个子目录是挂载点,那么不会对其进行遍历。
    • FTW_PHYS :默认情况下,nftw() 会对符号链接解引用,此标志则告诉 nftw() 不要这么做。
  • fpath 参数是正在被处理的文件的路径;sb 参数中包含该文件的相关信息;typeflag 参数可取值如下:

    • FTW_F:这是一个普通文件;
    • FTW_D:这是一个目录;
    • FTW_DNR:这是一个不能被读取的目录;
    • FTW_DP:正在对一个目录进行后续遍历,当前项是一个目录。
    • FTW_NS:对该文件调用 stat() 失败,sb 参数中的值未定义;
    • FTW_SL:这是一个符号链接,仅当使用 FTW_PHYS 标志时才会返回该值;
    • FTW_SLN:这是一个悬空的符号链接,仅当未使用 FTW_PHYS 标志时才会返回该值;
struct FTW {
    int base;		/* Offset to basename part of fpath */
    int level;		/* Depth of file within tree traversal */
};
  • 如果 fn() 返回 0,则继续进行遍历;如果 fn() 返回非 0,则立即停止遍历;fn() 的返回值就是 nftw() 的返回值。

  • 如果 flags 参数中指定了 FTW_ACTIONRETVAL,则 fn 的返回值可以为:

    • FTW_CONTINUE:继续遍历;
    • FTW_SKIP_SIBLINGS:不再处理当前目录中的条目,恢复对父目录的处理;
    • FTW_SKIP_SUBTREE:如果 fpath 是一个目录,那么就不对该目录下的条目调用 fn()
    • FTW_STOP:停止遍历,nftw() 返回 FTW_STOP

5. 当前工作目录

#include <unistd.h>

char *getcwd(char *buf, size_t size);
  • 获取当前工作目录。
  • 成功时返回 buf,失败时返回 NULL
  • 将当前工作目录以绝对路径的形式置于 buf 中(包含末尾空字符),buf 的大小为 size 字节,常设为 PATH_MAX
#include <unistd.h>

int chdir(const char *path);
  • 切换当前工作目录为 path
  • 成功时返回 0,失败时返回 -1。

6. 例子:读取目录

#include <iostream>
#include <dirent.h>

void test_read_dir(const char* dir_name) {
    DIR* dir = opendir(dir_name);
    if (!dir) {
        perror("opendir()");
        return;
    }

    struct dirent* ent;
    errno = 0;

    while (ent = readdir(dir)) {
        std::cout << ent->d_name << '\n';
    }

    if (errno != 0) {
        perror("readdir()");
    }

    closedir(dir);
}

int main() {
    test_read_dir(".");
    return 0;
}

7. 例子:遍历目录

#include <iostream>
#include <ftw.h>

int fn (const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
    for (int i = 0; i < ftwbuf->level; i++) {
        std::cout << '\t';
    }

    std::cout << fpath;
    if (typeflag == FTW_D) {
        std::cout << '/';
    }
    std::cout << '\n';

    return 0;
}

void walk_dir(const char* dir_name) {
    nftw(dir_name, fn, 10, 0);
}

int main() {
    walk_dir(".");
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值