在 Ubuntu(Linux)下使用 readdir 函数,需要包含以下头文件:
#include <dirent.h>
readdir 的函数原型在 <dirent.h> 中声明,典型用法如下:
#include <dirent.h>
#include <stdio.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;
}
补充说明:
opendir、readdir、closedir 都在 <dirent.h>struct dirent 也在 <dirent.h> 中定义errno(如 errno.h)或 perror,可加:#include <errno.h>
如果你指的是 readdir_r(线程安全版本)或 getdents 系统调用,也可以告诉我,我可以进一步说明。