温馨提示×

debian copendir头文件是什么

小樊
34
2026-09-01 01:17:04
栏目: 智能运维

在 Debian(以及其它 Linux 发行版、POSIX 系统)中,opendir 函数的头文件是:

#include <dirent.h>

说明

  • opendir 用于打开一个目录,返回一个 DIR *
  • 相关函数通常也都在 <dirent.h> 中:
    • opendir
    • readdir
    • closedir
    • rewinddir
    • seekdir
    • telldir

示例代码

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

int main(void) {
    DIR *dir = opendir("/tmp");
    if (!dir) {
        perror("opendir");
        return 1;
    }

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

    closedir(dir);
    return 0;
}

编译

在 Debian 上直接用 gcc 编译即可:

gcc test.c -o test

如果你是在写 C++,同样也是包含 <dirent.h>(或使用 <sys/types.h> + <dirent.h>)。

需要我补充 opendirfdopendir 的区别,或目录遍历的完整示例吗?

0