在 Debian(以及大多数 Linux 发行版)中,opendir() 是 C 语言里用来打开目录的函数,不是 shell 命令。下面分几种常见情况说明。
opendir()(最常见)#include <dirent.h>
#include <stdio.h>
#include <dirent.h>
#include <stdio.h>
int main() {
DIR *dir;
struct dirent *entry;
dir = opendir("."); // 打开当前目录
if (dir == NULL) {
perror("opendir");
return 1;
}
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() |
关闭目录 |
rewinddir() |
重新定位到目录开头 |
seekdir() / telldir() |
定位目录流 |
opendir 返回 NULLif (dir == NULL) {
perror("opendir");
}
常见原因:
opendir 是 C 函数,Shell 中一般用:
ls
cd
find
例如:
ls /home
opendirimport os
for name in os.listdir("."):
print(name)
opendir()是 C 语言 API,用于打开目录并配合readdir()遍历目录内容,在 Debian 下需要包含<dirent.h>并编译使用。
如果你是想:
可以把具体场景贴出来,我可以更精确地帮你。