我发现这里的另一个问题的答案非常有帮助。
sys/stat.h 库似乎有一个限制,因为当我尝试查看其他目录时,所有内容都被视为一个目录。
我想知道是否有人知道另一个系统功能,或者为什么它将当前工作目录之外的任何东西都视为一个目录。
我感谢任何人提供的任何帮助,因为这让我感到困惑,并且各种搜索都没有任何帮助。
我用来测试的代码是:
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
int main(void) {
int status;
struct stat st_buf;
struct dirent *dirInfo;
DIR *selDir;
selDir = opendir("../");
// ^ or wherever you want to look
while ((dirInfo = readdir(selDir))) {
status = stat (dirInfo->d_name, &st_buf);
if (S_ISREG (st_buf.st_mode)) {
printf ("%s is a regular file.\n", dirInfo->d_name);
}
if (S_ISDIR (st_buf.st_mode)) {
printf ("%s is a directory.\n", dirInfo->d_name);
}
}
return 0;
}