在 Debian(其实任何 Linux 系统都一样)里,copendir 并不是一个标准函数,所以你一调用就会报错,常见错误包括:
implicit declaration of function 'copendir'undefined reference to 'copendir'copendir was not declared in this scope没有 copendir 这个函数。
你可能是把下面这些函数记混了:
| 正确函数 | 作用 |
|---|---|
opendir() |
打开目录 |
readdir() |
读取目录项 |
closedir() |
关闭目录 |
scandir() |
扫描目录 |
#include <dirent.h>
#include <stdio.h>
int main() {
DIR *dir = opendir(".");
if (!dir) {
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
copendir 不存在opendir + readdir + closedircopendir,那大概率是笔误如果你愿意,可以把报错原文或代码贴出来,我可以精确告诉你哪里写错了。