Linux 高级编程 - 目录操作

xiaoxiao2021-02-27  174

Linux 目录简介

在 Linux 下一切皆是文件。目录也是文件,在 glibc 中定义了一些对目录项的操作,例如使用 opendir 打开目录,readdir 读取目录项等等,这篇文章介绍 Linux 目录相关的概念和操作。

目录项数据结构

就像文件有专门的数据结构表示一样,目录项在内核中也有相关表示。

目录项:struct dirent

#include <dirent.h> /* 目录结构体 */ struct dirent { ino_t d_ino; off_t d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; };

目录项名称(文件名)存储在 d_name 字段中。

目录流:DIR

就像 FILE 代表文件流一样,DIR 数据结构代表了一个目录流,这个结构定义在 dirent.h 中。

注意:你不需要主动为这些数据结构分配内存,目录的操作函数会完成内存的分配,你只需要在程序中使用指针引用就可以了。

常见目录 API

下面介绍一些常用的操作目录的 API。

1. opendir & closedir & readdir

首先来学习如何打开和关闭并读取一个目录:

#include <sys/types.h> #include <dirent.h> /* * name: 目录名 * return: 成功返回目录流指针,失败返回 NULL,并设置 errno */ DIR *opendir(const char *name); /* * dirp:目录流指针 * return:成功返回 0, 失败返回 -1, 并设置 errno */ int closedir(DIR *dirp); /* * dirp:目录流指针 * return:成功返回目录结构指针,失败返回空 */ struct dirent *readdir(DIR *dirp);

看一个打开和关闭目录的例子:

// list_dir.c #include <sys/types.h> #include <dirent.h> int main() { struct dirent* ep = NULL; // 打开目录 DIR *dp = opendir("./"); if (dp != NULL) { // 读取目录项 while (ep = readdir(dp)) puts(ep->d_name); closedir(dp); } return 0; }

编译运行,可以看到程序列出了当前文件下的文件名。

2. chdir & getcwd

chdir 用来改变调用进程的工作目录。

#include <unistd.h> /* * path:要改变的工作路径 * return:成功返回 0, 失败返回 -1 */ int chdir(const char *path);

getcwd 用来获取当前进程工作路径:

#include <unistd.h> /* * buf:存储当前工作目录的缓冲区 * size:缓冲区的长度 * return:成功返回指向当前工作目录的指针,失败返回 NULL,并设置 errno */ char *getcwd(char *buf, size_t size);

来看个实际的例子:

// test_chdir.c #include <stdio.h> #include <unistd.h> int main(void) { char buf[50] = { 0 }; printf("cur dir: %s\n", getcwd(buf, sizeof(buf))); if (0 == chdir("/home")) printf("change dir success.\n"); printf("cur dir: %s\n", getcwd(buf, sizeof(buf))); return 0; }

编译运行结果:

cur dir: (null) change dir success. cur dir: /home

工作路径成功修改了!

3. mkdir

在 shell 中 mkdir 可以创建目录,在 C 库中也有同名的库函数可以使用,来看看使用方法:

#include <sys/stat.h> #include <sys/types.h> /* * pathname: 要创建的目录路径 * mode:目录权限 * return:成功返回 0, 失败返回 -1 */ int mkdir(const char *pathname, mode_t mode);

例如下面的程序创建 hello_test 目录:

// test_mkdir.c #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> int main(void) { if (mkdir("./hello_test", 0755) == 0) printf("create hello_test success.\n"); else printf("create hello_test fail.\n"); return 0; }

结语

本次主要介绍目录相关的操作 API,基本使用方法不是很难,详细的用法参考 man 手册。

最后,感谢你的阅读,我们下次再见 :)

推荐关注我的微信公众号 CDeveloper,坚持技术原创,只说真话!

转载请注明原文地址: https://www.6miu.com/read-15116.html

最新回复(0)