在 Debian(以及大多数 Linux 发行版)中,copendir 并不是一个标准函数。你可能是想问 opendir / readdir / closedir 这一套目录遍历 API,或者把名字记混了。下面我按最常见的情况给你一个正确、完整、可用的说明。
opendir(不是 copendir)在 POSIX / Linux / Debian 中,打开目录使用的是:
#include <dirent.h>
DIR *opendir(const char *name);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main(void)
{
DIR *dir;
struct dirent *entry;
dir = opendir(".");
if (dir == NULL) {
perror("opendir");
return EXIT_FAILURE;
}
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
return EXIT_SUCCESS;
}
gcc dirlist.c -o dirlist
./dirlist
copendirDIR *dir = copendir(".");
✅ 不存在这个函数,会导致编译错误。
DIR *dir = opendir("/not/exist");
readdir(dir); // 危险
✅ 正确做法:
if (dir == NULL) {
perror("opendir");
return 1;
}
opendir(".");
// 忘记 closedir
✅ 会导致文件描述符泄漏。
. 和 ..(常见需求)if (entry->d_name[0] != '.') {
printf("%s\n", entry->d_name);
}
或更严谨:
if (strcmp(entry->d_name, ".") != 0 &&
strcmp(entry->d_name, "..") != 0) {
printf("%s\n", entry->d_name);
}
copendir 并不存在,如果你想:
cp -r(shell)man opendir
man readdir
如果没有:
sudo apt install man-db manpages-dev
✅ Debian 中 没有 copendir
✅ 正确使用的是:
opendirreaddirclosedir如果你愿意,可以告诉我:
我可以给你更精确的示例。