使用Visual Studio C ++按名称搜索目录中的文件 [英] Searching for files in a directory by name using Visual Studio C++

查看:121
本文介绍了使用Visual Studio C ++按名称搜索目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,在其中可以使用Visual Studio C ++在PC的目录中搜索某些文件.由于我对此并不十分了解,因此我在另一个答案中找到了此代码(如下),但找不到该代码的任何解释.我很难解决这个问题,将不胜感激.

I'm trying to create a program where I can search for some files in a directory on my PC, using Visual Studio C++. As I'm not very experienced with that, I found this code (below) in another answer but couldn't find any explanation to the code. I'm having a hard time figuring it out and would strongly appreciate any help possible.

如果还有另一种方法,我将很高兴知道如何做.谢谢!

If there's another way of doing this I would be pleased to know how. Thank you!

"现在您可以获得文件名.只需比较文件名即可.

" Now you can get file names. Just compare a file name.

 while ((dirp = readdir(dp)) != NULL) {
       std::string fname = dirp->d_name;
       if(fname.find("abc") != std::string::npos)
          files.push_back(fname);
    }

还可以使用scandir函数来注册过滤器功能.

Also you can use scandir function which can register filter function.

 static int filter(const struct dirent* dir_ent)
    {
        if (!strcmp(dir_ent->d_name, ".") || !strcmp(dir_ent->d_name, "..")) 
    return 0;
        std::string fname = dir_ent->d_name;

        if (fname.find("abc") == std::string::npos) return 0;

        return 1;
    }


    int main()
    {
        struct dirent **namelist;

        std::vector<std::string> v;
        std::vector<std::string>::iterator  it;

        n = scandir( dir_path , &namelist, *filter, alphasort );

        for (int i=0; i<n; i++) {
            std::string fname = namelist[i]->d_name;

            v.push_back(fname);

            free(namelist[i]);
        }
        free(namelist);

    return 0;
    }

"

推荐答案

更好的方法可能是使用新的 std :: filesystem . directory_iterators 允许您浏览以下内容目录.由于它们只是迭代器,因此您可以将它们与 std ::等标准算法结合使用find_if 搜索特定条目:

A better way of doing this would probably be using the new std::filesystem library. directory_iterators allow you to go through the contents of a directory. Since they are just iterators, you can combine them with standard algorithms like std::find_if to search for a particular entry:

#include <filesystem>
#include <algorithm>

namespace fs = std::filesystem;

void search(const fs::path& directory, const fs::path& file_name)
{
    auto d = fs::directory_iterator(directory);

    auto found = std::find_if(d, end(d), [&file_name](const auto& dir_entry)
    {
        return dir_entry.path().filename() == file_name;
    });

    if (found != end(d))
    {
        // we have found what we were looking for
    }

    // ...
}

我们首先为要搜索的目录创建一个 directory_iterator d .然后,我们使用 std :: find_if()浏览目录的内容,并搜索与我们要查找的文件名匹配的条目. std :: find_if()期望将一个函数对象作为最后一个参数,该参数应用于每个访问的元素,并且如果该元素与我们要查找的内容匹配,则返回 true . std :: find_if()将迭代器返回到该谓词函数为其返回 true 的第一个元素,否则返回结束迭代器.在这里,我们使用一个lambda作为谓词,当我们正在查看的目录条目路径的文件名部分与所需的文件名匹配时,该lambda返回 true .然后,我们将 std :: find_if()返回的迭代器与最终迭代器进行比较,以查看是否找到了条目.如果确实找到了一个条目,则 * found 将评估为 directory_entry 代表相应的文件系统对象.

We first create a directory_iterator d for the directory in which we want to search. We then use std::find_if() to go through the contents of the directory and search for an entry that matches the filename we are looking for. std::find_if() expects a function object as last argument that is applied to every visited element and returns true if the element matches what we are looking for. std::find_if() returns the iterator to the first element for which this predicate function returns true, otherwise it returns the end iterator. Here, we use a lambda as predicate that returns true when the filename component of the path of the directory entry we're looking at matches the wanted filename. Afterwards, we compare the iterator returned by std::find_if() to the end iterator to see if we have found an entry or not. In case we did find an entry, *found will evaluate to a directory_entry representing the respective file system object.

请注意,这将需要最新版本的Visual Studio2017.不要忘记将语言标准设置为/std:c ++ 17 /std:c ++项目属性(C ++/语言)中的最新.

Note that this will require a recent version of Visual Studio 2017. Don't forget to set the language standard to /std:c++17 or /std:c++latest in the project properties (C++/Language).

这篇关于使用Visual Studio C ++按名称搜索目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆