在 Ubuntu(以及一般 Linux 系统)中,opendir 函数的头文件是:
#include <dirent.h>
opendir 用于打开一个目录流,函数声明就在 <dirent.h> 中。
示例:
#include <stdio.h>
#include <dirent.h>
int main() {
DIR *dir = opendir(".");
if (dir == NULL) {
perror("opendir");
return 1;
}
closedir(dir);
return 0;
}
补充说明:
opendir 属于 POSIX 标准,不是 C 标准库本身glibc 提供find /usr/include -name dirent.h
一般会看到:/usr/include/dirent.h
如果你是在写 C/C++ 程序,直接 #include <dirent.h> 即可。