在 Ubuntu(或者说 Linux / POSIX 系统)里,没有 copendir 这个函数,所以你会“找不到”。
你可能想用的是下面这些目录相关函数之一:
#include <dirent.h>
DIR *opendir(const char *name);
opendirDIR *copendirstruct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);
你可能是把下面这些搞混了:
| 你可能想的 | 实际函数 |
|---|---|
copendir |
opendir |
copen |
open / fopen |
cfopen |
fopen |
Linux 里很多“复制”函数是 c 开头(如 calloc、creat),但 目录操作不是。
#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
Python 也没有 copendir,你可能是想:
import os
os.listdir(".")
或
with os.scandir(".") as it:
for entry in it:
print(entry.name)
如果你能贴一下你写的代码或报错信息,我可以直接告诉你哪里写错了。