在 Ubuntu(Linux)下,opendir 本身不能递归。
简单说明:
opendir() 只是打开一个目录,返回一个 DIR *,用于配合 readdir() 读取该目录下的条目。opendir → readdirstat / lstat 判断是否是目录opendir(递归或栈/队列实现)示例思路(C 伪代码):
void list_dir(const char *path) {
DIR *dir = opendir(path);
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 ||
strcmp(entry->d_name, "..") == 0)
continue;
char full[PATH_MAX];
snprintf(full, sizeof(full), "%s/%s", path, entry->d_name);
struct stat st;
lstat(full, &st);
if (S_ISDIR(st.st_mode)) {
list_dir(full); // 递归
} else {
printf("%s\n", full);
}
}
closedir(dir);
}
补充:
nftw()(C 标准库)或命令行 findopendir 只是“打开目录”,递归是算法层面的事如果你需要 完整可编译的 C 示例 或 用 nftw 的写法,我可以给你。