在 Debian(以及一般的 Linux / POSIX 系统) 中,使用 readdir 需要包含以下头文件:
#include <dirent.h>
#include <stdio.h>
#include <dirent.h>
int main() {
DIR *dir = opendir(".");
if (!dir) {
perror("opendir");
return 1;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
return 0;
}
readdir 函数原型:struct dirent *readdir(DIR *dirp);
DIR → <dirent.h>struct dirent → <dirent.h>opendir, closedir → <dirent.h>gcc test.c -o test
如果你指的是 readdir_r(线程安全版本)或 getdents,可以告诉我,我可以补充对应头文件和区别。