温馨提示×

什么头文件含ubuntu copendir

小樊
31
2026-09-14 18:50:34
栏目: 智能运维

Ubuntu(Linux) 下,使用 opendir() 函数需要包含以下头文件:

#include <dirent.h>

opendir() 是 POSIX 标准函数,用于打开一个目录流,定义就在 <dirent.h> 中。

常见相关函数也都在该头文件中:

  • opendir()
  • readdir()
  • closedir()
  • rewinddir()

示例:

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

int main() {
    DIR *dir = opendir(".");
    if (dir == NULL) {
        perror("opendir");
        return 1;
    }

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

    closedir(dir);
    return 0;
}

编译(Ubuntu):

gcc test.c -o test

如果你是在 C++ 里用,也可以包含 <dirent.h>,它是 C 标准兼容的。

0