【发布时间】:2020-10-09 17:19:48
【问题描述】:
我正在尝试计算给定路径中的文件和子文件夹的数量。我做了一些研究,但人们主要是为 Linux 做的,但我需要在 Windows 中做。这是我的代码:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
void listFiles(const char *path);
int main()
{
char path[100];
printf("Enter path to list files: ");
scanf("%s", path);
listFiles(path);
return 0;
}
void listFiles(const char *path)
{
struct dirent *dp;
DIR *dir = opendir(path);
int fileCount = 0;
int dirCount = 0;
int i, n;
if (!dir)
return;
for(i = 1; i < n; i++){
while ((dp = readdir(dir)) != NULL)
{
if(dir->d_type == DT_REG){
fileCount++;
}
if(dir->d_type == DT_DIR)
dirCount++;
}
printf("%s: file count is: %d and dir count is: %d\n",path, fileCount, dirCount);
}
closedir(dir);
}
这是我无法修复的错误:
28 14 [Error] 'DIR' has no member named 'd_type'
28 26 [Error] 'DT_REG' undeclared (first use in this function)
28 26 [Note] each undeclared identifier is reported only once for each function it appears in
31 17 [Error] 'DIR' has no member named 'd_type'
31 29 [Error] 'DT_DIR' undeclared (first use in this function)
28 recipe for target '2.o' failed
你能帮我解决它吗?我看到了一些关于 _BSD_SOURCE 的东西,但我不知道它是 Linux 还是 Windows。
【问题讨论】:
-
"我正在尝试计算给定路径中的文件和子文件夹的数量。"嗯,没有。实际上,我认为您正在尝试在不适合的环境中编译您一无所知的代码。请说明您从哪里获得的来源。
-
它的副本。这个问题在 SO 中多次询问和回答。看到这个链接。 list dir。我已经回答了这个问题。
-
我从codeforwin.org/2018/03/… 找到了这段代码,它是用于列出的,我正在尝试将其调整为计数不列出。 @yunnosch
-
列出一个目录 == 来绘制和计算目录组件