Quantcast
Channel: 小蓝博客
Viewing all articles
Browse latest Browse all 3155

linux中的目录操作函数

$
0
0

Linux中的目录操作函数

在Linux系统编程中,目录操作是一个常见的任务。目录操作包括创建、删除、读取目录内容以及遍历目录树等。本文将详细介绍Linux中常用的目录操作函数,包括其用法和示例代码。

一、创建目录

1.1 mkdir函数

mkdir函数用于创建一个新目录。其原型定义在 <sys/stat.h>头文件中:

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

int mkdir(const char *pathname, mode_t mode);
  • pathname:要创建的目录路径。
  • mode:新目录的权限位。

示例代码:

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

int main() {
    const char *dir = "/tmp/newdir";
    mode_t mode = 0755;

    if (mkdir(dir, mode) == 0) {
        printf("Directory created successfully.\n");
    } else {
        perror("mkdir");
    }

    return 0;
}

二、删除目录

2.1 rmdir函数

rmdir函数用于删除一个空目录。其原型定义在 <unistd.h>头文件中:

#include <unistd.h>

int rmdir(const char *pathname);
  • pathname:要删除的目录路径。

示例代码:

#include <unistd.h>
#include <stdio.h>

int main() {
    const char *dir = "/tmp/newdir";

    if (rmdir(dir) == 0) {
        printf("Directory deleted successfully.\n");
    } else {
        perror("rmdir");
    }

    return 0;
}

三、读取目录内容

3.1 opendir函数

opendir函数用于打开一个目录。其原型定义在 <dirent.h>头文件中:

#include <dirent.h>

DIR *opendir(const char *name);
  • name:要打开的目录路径。

3.2 readdir函数

readdir函数用于读取目录中的下一个条目。其原型定义在 <dirent.h>头文件中:

#include <dirent.h>

struct dirent *readdir(DIR *dirp);
  • dirp:由 opendir返回的目录指针。

3.3 closedir函数

closedir函数用于关闭一个目录。其原型定义在 <dirent.h>头文件中:

#include <dirent.h>

int closedir(DIR *dirp);
  • dirp:由 opendir返回的目录指针。

示例代码:

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

int main() {
    const char *dir = "/tmp";
    DIR *dp;
    struct dirent *entry;

    dp = opendir(dir);
    if (dp == NULL) {
        perror("opendir");
        return 1;
    }

    while ((entry = readdir(dp)) != NULL) {
        printf("Name: %s\n", entry->d_name);
    }

    closedir(dp);
    return 0;
}

四、遍历目录树

4.1 nftw函数

nftw函数用于遍历目录树。其原型定义在 <ftw.h>头文件中:

#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);
  • dirpath:要遍历的目录路径。
  • fn:对每个文件调用的函数指针。
  • nopenfd:同时打开的文件描述符的最大数量。
  • flags:控制遍历行为的标志。

示例代码:

#include <ftw.h>
#include <stdio.h>
#include <sys/stat.h>

int display_info(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
    printf("Name: %s\n", fpath);
    return 0;
}

int main() {
    const char *dirpath = "/tmp";

    if (nftw(dirpath, display_info, 20, 0) == -1) {
        perror("nftw");
        return 1;
    }

    return 0;
}

五、获取和修改目录属性

5.1 stat函数

stat函数用于获取文件或目录的属性。其原型定义在 <sys/stat.h>头文件中:

#include <sys/stat.h>

int stat(const char *pathname, struct stat *statbuf);
  • pathname:文件或目录路径。
  • statbuf:用于存储文件属性的结构体。

5.2 chmod函数

chmod函数用于修改文件或目录的权限。其原型定义在 <sys/stat.h>头文件中:

#include <sys/stat.h>

int chmod(const char *pathname, mode_t mode);
  • pathname:文件或目录路径。
  • mode:新的权限位。

示例代码:

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

int main() {
    const char *dir = "/tmp/newdir";
    struct stat statbuf;

    if (stat(dir, &statbuf) == 0) {
        printf("Permissions: %o\n", statbuf.st_mode & 0777);
    } else {
        perror("stat");
    }

    if (chmod(dir, 0755) == 0) {
        printf("Permissions changed successfully.\n");
    } else {
        perror("chmod");
    }

    return 0;
}

六、总结

本文详细介绍了Linux系统编程中常用的目录操作函数,包括创建目录、删除目录、读取目录内容、遍历目录树以及获取和修改目录属性。这些函数是进行文件系统操作的基础,通过示例代码展示了其具体用法。希望本文能帮助您更好地理解和应用这些目录操作函数,提高系统编程的效率和能力。


Viewing all articles
Browse latest Browse all 3155

Latest Images

Trending Articles