在 Debian(以及其他 Linux 系统)上,使用 opendir() 需要包含以下头文件:
#include <dirent.h>
#include <sys/types.h> // 通常也需要,定义 DIR、ino_t 等
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
int main(void) {
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;
}
gcc test.c -o test
./test
opendir():打开目录,返回 DIR *readdir():读取目录项closedir():关闭目录如果你是想问 “怎么安装缺少的头文件”(比如编译时提示 dirent.h: No such file or directory),可以告诉我具体报错,我可以帮你查是缺哪个 dev 包。