【问题标题】:Count files with a specific extension in directories and subdirectories C++计算目录和子目录中具有特定扩展名的文件 C++
【发布时间】:2017-05-09 15:45:55
【问题描述】:

例如:.txt

C:\Duck\aa.txt
C:\Duck\Dog\Pig\cc.txt
C:\Duck\Dog\kk.txt
C:\Duck\Cat\zz.txt
C:\xx.txt

返回 5

【问题讨论】:

  • 寻求帮助时,最好提供您尝试过的代码和遇到的错误。要求社区为您编写代码是不礼貌的。
  • 这个问题显示了自己寻找解决方案的零努力,包括在此处发布之前未能通过 Google 或本网站进行基本搜索。在目前的状态下,它对未来的读者绝对没有价值,应该关闭。
  • 我得到的最多的是答案中的代码。很抱歉之前没有显示我的代码,是的,我已经搜索过了,但我找不到将 .txt 包含在计数的子目录中的方法,但我已经在 Dac Saunders 的帮助下进行了管理。谢谢大家。

标签: c++ recursion directory subdirectory


【解决方案1】:

您必须使用opendir()readdir()

例子:

DIR *dir = opendir("."); // current dir
if (dir == NULL) {
  perror("opendir: .");
  return 1;
}

struct dirent *dirent;
while ((dirent = readdir(dir)) != NULL) {
  printf("%s\n", dirent->d_name);
}

closedir(dir);

【讨论】:

    【解决方案2】:

    请试一试。

    #include <sys/types.h>
    #include <dirent.h>
    #include <stdio.h>
    #include <string.h>
    int count = 0;
    void listdir(const char *name, int level)
    {
    
        DIR *dir;
        struct dirent *entry;
        if (!(dir = opendir(name)))
            return;
        if (!(entry = readdir(dir)))
            return;
    
        do {
            if (entry->d_type == DT_DIR) {
                char path[1024];
                int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
                path[len] = 0;
                if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                    continue;
                listdir(path, level + 1);
            }
            else {
                const char *ext = strrchr(entry->d_name,'.');
                if((!ext) || (ext == entry->d_name))
                    ;
                else {
                    if(strcmp(ext, ".txt") == 0) {
                        printf("%*s- %s\n", level * 2, "", entry->d_name);
                        count++;
                    }
                }
    
            }
        } while (entry = readdir(dir));
        closedir(dir);
    
    }
    
    int main(void)
    {
        listdir(".", 0);
        printf("There are %d .txt files in this directory and its subdirectories\n", count);
        return 0;
    }
    

    测试

    ./a.out
    - hi.txt
    - payfile.txt
    - csis.txt
    - CMakeCache.txt
    - text.txt
    - sorted.txt
    - CMakeLists.txt
    - ddd.txt
    - duom.txt
    - output.txt
          - mmap_datafile.txt
          - file_datafile.txt
      - CMakeLists.txt
        - link.txt
      - TargetDirectories.txt
    There are 15 .txt files in this directory and its subdirectories
    

    【讨论】:

      【解决方案3】:

      谢谢大家的回答,你试过的代码:

      WIN32_FIND_DATA fdata;
      int counter = 0;
      if (HANDLE first = FindFirstFile(L"C:\\duck\\*.txt", &fdata))
      {
          counter++;
          do 
          {
              counter++;
          } while (FindNextFile(first, &fdata));
      }
      

      但是正如你所看到的,我没有得到我想要的结果,即包含子目录。 多亏了“Dac Saunders”和其他所有人,它帮了很多忙。

      【讨论】:

      • 请发送tour。如果你自己回答你的问题。答案必须是解决方案。你的回答很糟糕,因为我不懂你的英语,你说"The most I got was the code that is in my answer.",所以显然这段代码不起作用。如果您想添加代码,请尝试edit 您的问题并删除此答案。或者edit这个答案和你的最终解决方案。
      猜你喜欢
      • 2010-12-28
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 2021-06-08
      • 2014-09-21
      • 1970-01-01
      • 2010-09-06
      • 1970-01-01
      相关资源
      最近更新 更多