美文网首页Linux从 0 开始学习 Linux 系列程序员
从 0 开始学习 Linux 系列之「15.目录操作」

从 0 开始学习 Linux 系列之「15.目录操作」

作者: 登龙zZ | 来源:发表于2017-08-05 08:52 被阅读73次
DIR

版权声明:本文为 cdeveloper 原创文章,可以随意转载,但必须在明确位置注明出处!

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

shellmkdir 可以创建目录,在 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 手册。

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

相关文章

  • 从 0 开始学习 Linux 系列之「15.目录操作」

    版权声明:本文为 cdeveloper 原创文章,可以随意转载,但必须在明确位置注明出处! Linux 目录简介 ...

  • 开发工具

    从0开始学习 GITHUB 系列之「初识 GITHUB」 从0开始学习 GITHUB 系列之「加入 GITHUB」...

  • GitHub

    转载: 从 0 开始学习 GitHub 系列之「初识 GitHub」 从 0 开始学习 GitHub 系列之「加入...

  • Git 和 Github 教程

    从0开始学习 GITHUB 系列的收录整理 1, 从0开始学习 GITHUB 系列之「初识 GITHUB」2, 从...

  • git---成长之路

    从0开始学习 GitHub 系列之「01.初识 GitHub」 - stormzhang的专栏 - ...

  • Linux操作系列之03---VirtualBox虚拟机的网络设

    Linux操作文档系列之03 VirtualBox虚拟机的网络设置 目录:1.VBox网络模式2.配置Linux服...

  • Linux命令

    Linux 操作命令 文件路径操作从当前目录开始 文件路径操作从更目录上一级目录 文件权限 读,写,执行分别用r,...

  • 20170724GitHub继续

    学习内容 从 0 开始学习 GitHub 系列之【Git 进阶】 作者:stormzhang 设置git本地的用...

  • github的使用

    从0开始学习 GITHUB 系列之「GIT 速成」 http://stormzhang.com/github/20...

  • 从 0 开始学习 Linux 系列之「02.如何安装 Linux

    版权声明:本文为 cdeveloper 原创文章,可以随意转载,但必须在明确位置注明出处! 前言 之前的一篇文章 ...

网友评论

    本文标题:从 0 开始学习 Linux 系列之「15.目录操作」

    本文链接:https://www.haomeiwen.com/subject/eedilxtx.html