文件目录的相关操作

opendir 函数

包含头文件: 确保在代码中包含必要的头文件。

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

打开目录: 使用 opendir 函数打开目录。该函数返回一个指向 DIR 类型的指针。

DIR *dir = opendir("/path/to/directory");
if (dir == NULL) {
    perror("opendir");
    return 1;
}

读取目录内容: 使用 readdir 函数读取目录内容。该函数返回一个指向 struct dirent 类型的指针。

struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
    printf("%s\n", entry->d_name);
}

关闭目录: 使用 closedir 函数关闭目录。

closedir(dir);
  • opendir(const char *name): 打开指定名称的目录,返回指向 DIR 结构的指针。如果失败,返回 NULL
  • readdir(DIR *dirp): 读取目录中的下一个条目,返回指向 struct dirent 结构的指针。如果读取到目录末尾或发生错误,返回 NULL
  • closedir(DIR *dirp): 关闭由 opendir 打开的目录流。
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>

int main() {
    DIR *dir;
    struct dirent *entry;

    // 打开目录
    dir = opendir("/path/to/directory");
    if (dir == NULL) {
        perror("opendir");
        return 1;
    }

    // 读取目录内容
    while ((entry = readdir(dir)) != NULL) {
        printf("%s\n", entry->d_name);
    }

    // 关闭目录
    closedir(dir);

    return 0;
}

 

struct dirent 结构

struct dirent 结构定义在 <dirent.h> 头文件中,包含以下成员:

struct dirent {
    ino_t          d_ino;       /* inode number */
    off_t          d_off;       /* offset to the next dirent */
    unsigned short d_reclen;    /* length of this record */
    unsigned char  d_type;      /* type of file */
    char           d_name[256]; /* filename */
};

常用成员包括:

  • d_name: 文件名。
  • d_type: 文件类型。
  • DT_BLK: 块设备
  • DT_CHR: 字符设备
  • DT_DIR: 目录
  • DT_FIFO: FIFO (命名管道)
  • DT_LNK: 符号链接
  • DT_REG: 常规文件
  • DT_SOCK: 套接字
  • DT_UNKNOWN: 未知类型

readdir 函数

包含头文件: 确保在代码中包含必要的头文件。

#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

打开目录: 使用 opendir 函数打开目录。该函数返回一个指向 DIR 类型的指针。

DIR *dir = opendir("/path/to/directory");
if (dir == NULL) {
    perror("opendir");
    return 1;
}
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

读取目录内容: 使用 readdir 函数读取目录内容。该函数返回一个指向 struct dirent 类型的指针。

struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
    printf("%s\n", entry->d_name);
}
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

关闭目录: 使用 closedir 函数关闭目录。

closedir(dir);
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

closedir 函数

int closedir(DIR *dirp);
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
  • 参数: dirp 是一个指向 DIR 结构的指针,表示要关闭的目录流。
  • 返回值: 如果成功,返回 0。如果失败,返回 -1,并设置 errno 以指示错误。

使用步骤

  1. 打开目录: 使用 opendir 函数打开目录。
  2. 读取目录内容: 使用 readdir 函数读取目录内容。
  3. 关闭目录: 使用 closedir 函数关闭目录。

chdir

chdir 用于更改当前工作目录。

 函数声明

int chdir(const char *path);
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
参数
  • path: 目标目录的路径。
返回值
  • 成功返回 0
  • 失败返回 -1,并设置 errno
#include <unistd.h>
#include <stdio.h>

int main() {
    if (chdir("/path/to/directory") == -1) {
        perror("chdir");
        return 1;
    }
    // 成功更改工作目录
    return 0;
}
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

 

getcwd

getcwd 用于获取当前工作目录的路径。

函数声明
char *getcwd(char *buf, size_t size);
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
参数
  • buf: 存储路径的缓冲区。
  • size: 缓冲区的大小。
返回值
  • 成功返回 buf
  • 失败返回 NULL,并设置 errno

 

mkdir

mkdir 用于创建新目录。

函数声明
int mkdir(const char *pathname, mode_t mode);
参数
  • pathname: 要创建的目录的路径。
  • mode: 目录的权限位。
返回值
  • 成功返回 0
  • 失败返回 -1,并设置 errno
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>

int main() {
    if (mkdir("/path/to/directory", 0755) == -1) {
        perror("mkdir");
        return 1;
    }
    // 成功创建目录
    return 0;
}

 

rmdir

rmdir 用于删除空目录。

函数声明
int rmdir(const char *pathname);
参数
  • pathname: 要删除的目录的路径。
返回值
  • 成功返回 0
  • 失败返回 -1,并设置 errno
#include <unistd.h>
#include <stdio.h>

int main() {
    if (rmdir("/path/to/directory") == -1) {
        perror("rmdir");
        return 1;
    }
    // 成功删除目录
    return 0;
}

 

stat

stat 用于获取文件或目录的状态信息。

函数声明
int stat(const char *pathname, struct stat *statbuf);
参数
  • pathname: 文件或目录的路径。
  • statbuf: 存储状态信息的结构体指针。
返回值
  • 成功返回 0
  • 失败返回 -1,并设置 errno
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>

int main() {
    struct stat sb;
    if (stat("/path/to/file_or_directory", &sb) == -1) {
        perror("stat");
        return 1;
    }

    printf("File size: %lld bytes\n", (long long) sb.st_size);
    printf("Permissions: %o\n", sb.st_mode & 0777);
    printf("Last modified: %ld\n", (long) sb.st_mtime);
    return 0;
}

  • 29
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值