I am trying to count numbers of files and subfolders in given path. I did some research but people did it mostly for Linux but I need to do it in Windows. This is my code:
#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);
}
And this is errors I couldn't fix:
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
Can you please help me how to fix it. I saw something about _BSD_SOURCE but I don't know if it's Linux or Windows.