【问题标题】:How to count numbers of files and subfolders using C (Windows)如何使用 C (Windows) 计算文件和子文件夹的数量
【发布时间】: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。

【问题讨论】:

  • 这能回答你的问题吗? Counting number of files and folders in directory
  • "我正在尝试计算给定路径中的文件和子文件夹的数量。"嗯,没有。实际上,我认为您正在尝试在不适合的环境中编译您一无所知的代码。请说明您从哪里获得的来源。
  • 它的副本。这个问题在 SO 中多次询问和回答。看到这个链接。 list dir。我已经回答了这个问题。
  • 我从codeforwin.org/2018/03/… 找到了这段代码,它是用于列出的,我正在尝试将其调整为计数不列出。 @yunnosch
  • 列出一个目录 == 来绘制和计算目录组件

标签: c windows dirent.h


【解决方案1】:

您的标题问题表明需要在 Windows 上执行您正在执行的操作,但您发布的某些代码(例如 readdir())需要符合 POSIX。它不适用于 Windows。

可用于对文件和目录进行计数的 Windows 函数包括:

FindFirstFile

FindNextFile

GetFileAttributes

这是一个基本框架,可帮助您入门。它确实可以编译,但需要进行调整才能为您工作。您的适应将需要向下遍历找到的任何目录,以搜索更多文件/子目录,然后返回到顶层。如图所示,您可以使用属性来决定找到的 file 是 file 还是 directory,并增加适当的计数器以跟踪每个:(此示例只计算一级目录和文件。)

根据您的环境添加#include 文件,其中必须包括#include &lt;windows.h&gt; 等。

#include <windows.h>
//include other files as needed for your system

int main(int argc, char *argv[])
{
    FILE *out;
    int fileCount = 0;
    int dirCount = 0;


    DWORD attribute;

    WIN32_FIND_DATA file;
    HANDLE fhandle;

    out = fopen("file_list.txt", "w");
    if(out == NULL){
        printf( "Error creating file\n");
        return 0;
    }
    // Place your own starting point here.  
    fhandle = FindFirstFile("C:\\play\\*", &file);
    if(fhandle == INVALID_HANDLE_VALUE){
        printf("Invalid File Handle.\n");
        return 0;
    } 

    while(FindNextFile(fhandle, &file) != 0){
        attribute = GetFileAttributes(file.cFileName);
        switch(attribute) {
            case FILE_ATTRIBUTE_DIRECTORY:
                dirCount++;
                fprintf(out, "DIR - %s\n", file.cFileName);
                break;
            default:
                fileCount++;
                fprintf(out, "FILE - %s\n", file.cFileName);
                break;
        }                                                
    }
    fprintf(out, "Directories: %d\nFiles      :%d\n", dirCount, fileCount);
    fclose(out);

    return 0;
}

【讨论】:

    猜你喜欢
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多