#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <dirent.h>
int main(int argc, char const *argv[]){
struct dirent **namelist;
int n;
n = scandir(".", &namelist, NULL, alphasort);
printf("Number Files %d\n", n);
if (n == -1){
perror("scandir");
exit(EXIT_FAILURE);
}
while (n--){
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
}
我收到 alphasort 错误,“错误:'alphasort' 未声明(首次在此函数中使用)”,并且收到函数 'scandir' 隐式声明错误。其他程序成功编译并包含库。
我已确保我的计算机上有 dirent.h 库。 VSCode 的快速修复是编辑包含路径设置,但即使我将路径添加到 C:\MinGW\x86_64-w64-mingw32\include ,其中包含的库也无法编译。
MinGW 似乎不支持
scandir
。它一般不会尝试提供 POSIX API,提供 opendir
的事实已经有点令人惊讶了。