在 Debian(以及所有 Linux) 里,opendir() 是 C 语言标准库(POSIX) 提供的函数,不是 shell 命令,也不是 Debian 特有的工具。下面分几种常见情况说明“怎么调用”。
opendir#include <dirent.h>
#include <stdio.h>
#include <stdlib.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;
}
gcc test.c -o test
./test
opendir 属于 glibc,Debian 默认已安装。
确认:
dpkg -l | grep libc6
如果你在 最小化系统 或 交叉编译,可能需要:
sudo apt install libc6-dev
DIR *dir = opendir(path);
if (!dir) {
perror("opendir");
}
ls -ld 目录名
ls
import os
for name in os.listdir("."):
print(name)
#include <filesystem>
for (auto &p : std::filesystem::directory_iterator(".")) {
std::cout << p.path() << std::endl;
}
Debian 不能直接“调用 opendir”,它只能在 C/C++ 程序中通过
#include <dirent.h>使用。
如果你是想:
可以告诉我具体场景,我可以给你更精准的示例。