在 Ubuntu(Linux) 下使用 opendir(),需要包含以下头文件:
#include <dirent.h>
opendir() 用于打开一个目录流,函数原型为:
DIR *opendir(const char *name);
返回值是 DIR *,失败时返回 NULL。
#include <stdio.h>
#include <dirent.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
| 函数 | 所需头文件 |
|---|---|
opendir |
<dirent.h> |
readdir |
<dirent.h> |
closedir |
<dirent.h> |
DIR 类型 |
<dirent.h> |
如果你是在 C++ 或 交叉编译环境 下使用,也可以告诉我,我可以给你更具体的说明。