在 Ubuntu(以及其它类 Unix 系统)中,opendir 函数声明在头文件:
#include <dirent.h>
示例:
#include <dirent.h>
#include <stdio.h>
int main() {
DIR *dir = opendir(".");
if (dir == NULL) {
perror("opendir");
return 1;
}
closedir(dir);
return 0;
}
补充说明:
opendir 用于打开目录,返回 DIR *readdir、closedir 等)也都在 <dirent.h> 中man opendir 查看详细说明(需安装 manpages-dev)如果你是想问“实现文件在哪”(源码位置),那通常在 glibc 中,例如:
/sysdeps/posix/opendir.c
但普通开发只需要包含 <dirent.h> 即可。