Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++。find()函数在目录中搜索文件名_C++_Visual Studio_Search_Directory_Filesystems - Fatal编程技术网

C++ C++。find()函数在目录中搜索文件名

C++ C++。find()函数在目录中搜索文件名,c++,visual-studio,search,directory,filesystems,C++,Visual Studio,Search,Directory,Filesystems,我正在尝试开发一个小型搜索引擎应用程序,通过键入本地文件的名称作为输入,在我的C://目录中搜索本地文件 我收到这个建议是为了实现搜索功能。但是,它只允许我使用文件的确切名称进行搜索,例如“1234_0506AB.pdf” 我希望我的搜索功能将“1234”作为输入,并且仍然能够获取“1234_0506AB.pdf” 另一个建议是:不要简单地通过dir\u entry.path().filename()==file\u name测试相等性,只需将文件名作为stringdir\u entry.pat

我正在尝试开发一个小型搜索引擎应用程序,通过键入本地文件的名称作为输入,在我的C://目录中搜索本地文件

我收到这个建议是为了实现搜索功能。但是,它只允许我使用文件的确切名称进行搜索,例如“1234_0506AB.pdf”

我希望我的搜索功能将“1234”作为输入,并且仍然能够获取“1234_0506AB.pdf”

另一个建议是:不要简单地通过
dir\u entry.path().filename()==file\u name
测试相等性,只需将文件名作为string
dir\u entry.path().filename().string()
并执行
.find()
。对于更一般的搜索,您可以使用regex并将其与文件名匹配

我在这方面的经验很少,需要一些帮助和指导才能在代码中使用.find()或regex

#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
    }

    // ...
}
#包括
#包括
名称空间fs=std::filesystem;
无效搜索(常量fs::路径和目录,常量fs::路径和文件名)
{
auto d=fs::directory\u迭代器(directory);
自动查找=标准::查找if(d,end(d),[&文件名](const auto&dir\u条目)
{
return dir\u entry.path().filename()==文件名;
});
如果(找到!=结束(d))
{
//我们找到了我们要找的东西
}
// ...
}

要使用find,您应该检查,其中还包含大多数网站上的一些示例

在代码中,如果文件名与要查找的文件名完全相同,则接受该文件名:

return dir_entry.path().filename() == file_name;
要接受子字符串匹配,您必须修改此检查以使用
find
而不是
=
。如链接文档中所述,
find
如果找不到匹配项,则返回
npos

return dir_entry.path().filename().string().find(file_name.string()) != std::string::npos;
如果仅在字符串开头查找匹配项,则可以使用
==0
而不是
!=非营利组织

但在这种情况下,您也可以使用其他选项,例如使用将文件名剪切到所需的长度:

return dir_entry.path().filename().string().substr(0, file_name.size()) == file_name.string();

对于使用regex的解决方案,请在同一站点上检查。

要使用find,应检查,其中还包含大多数站点上的一些示例

在代码中,如果文件名与要查找的文件名完全相同,则接受该文件名:

return dir_entry.path().filename() == file_name;
要接受子字符串匹配,您必须修改此检查以使用
find
而不是
=
。如链接文档中所述,
find
如果找不到匹配项,则返回
npos

return dir_entry.path().filename().string().find(file_name.string()) != std::string::npos;
如果仅在字符串开头查找匹配项,则可以使用
==0
而不是
!=非营利组织

但在这种情况下,您也可以使用其他选项,例如使用将文件名剪切到所需的长度:

return dir_entry.path().filename().string().substr(0, file_name.size()) == file_name.string();


有关使用regex的解决方案,请在同一站点上查看。

您是否尝试过使用regex?不幸的是,没有,我以前从未使用过它如果search str是
1234
并且文件名是
aaa1234aaa
它也应该找到它吗?不只是从字符串的开头开始,然后是:1234和1234aaaaaaaif你在同一个目录上多次这样做,然后,您可能想将文件名复制到
std::set
中,并使用
下限进行查找。您尝试使用regex了吗?不幸的是,没有,我以前从未使用过它如果search str是
1234
并且文件名是
aaa1234aaa
它也应该找到它吗?不只是从字符串的开头开始,然后是:1234和1234aaaaaaaif你在同一个目录上多次这样做,然后,您可能希望将文件名复制到
std::set
中,并使用
下限进行查找,而不是
!=0
NPO
但这在Q中并不完全清楚。事实上,如果是这样的话,
查找
可能不是最好的选择你可能是对的-我假设他想查找任何包含字符串的文件,但看看他的示例,这也是可能的。我想查找任何以字符串开头的文件。我在尝试替换
return dir\u entry.path().filename()==file\u name
by
return dir\u entry.path().filename().string().find(文件名)!=std::string::npos错误C2664'unsigned int std::basic_string::find(常量std::basic_string&,常量unsigned int)noexcept const”:无法将参数1从“const std::filesystem::path”转换为“const char”。我错过了该文件。\u名称也是一个路径而不是字符串,编辑了我的答案。可能OP需要
==0
而不是
!=NPO
但这在Q中并不完全清楚。事实上,如果是这样的话,
查找
可能不是最好的选择你可能是对的-我假设他想查找任何包含字符串的文件,但看看他的示例,这也是可能的。我想查找任何以字符串开头的文件。我在尝试替换
return dir\u entry.path().filename()==file\u name
by
return dir\u entry.path().filename().string().find(文件名)!=std::string::npos错误C2664'unsigned int std::basic_string::find(常量std::basic_string&,常量unsigned int)noexcept const”:无法将参数1从“const std::filesystem::path”转换为“const char”。我错过了该文件。\u名称也是路径而不是字符串,编辑了我的答案。