Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.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++;递归搜索具有特定扩展名的文件_C++_Windows_Winapi_Recursion_Visual C++ - Fatal编程技术网

C++ C++;递归搜索具有特定扩展名的文件

C++ C++;递归搜索具有特定扩展名的文件,c++,windows,winapi,recursion,visual-c++,C++,Windows,Winapi,Recursion,Visual C++,我正在尝试编写一个函数,以获取文件扩展名的std::set,并递归搜索具有这些扩展名的文件,将路径写入文件: auto get_files(_In_ const std::wstring root, // root dir of search _In_ const std::set<std::string> &ext, // extensions to search for) _Out_ std::wofstream &ret /* f

我正在尝试编写一个函数,以获取文件扩展名的
std::set
,并递归搜索具有这些扩展名的文件,将路径写入文件:

auto get_files(_In_ const std::wstring root, // root dir of search
        _In_ const std::set<std::string> &ext, // extensions to search for)
        _Out_ std::wofstream &ret /* file to write paths to */) -> int
{
    HANDLE find = INVALID_HANDLE_VALUE;

    // check root path
    {
        LPCWSTR root_path = root.c_str();
        DWORD root_attrib = GetFileAttributesW(root_path);
        if(root_attrib == INVALID_FILE_ATTRIBUTES) return 1; // root doesn't exist
        if(!(root_attrib & FILE_ATTRIBUTE_DIRECTORY)) return 2; // root isn't a directory
    }

    LPCWSTR dir;
    WIN32_FIND_DATAW fd;

    // dir concat
    {
        std::wstring x = root.c_str();
        x.append(L"\\*");
        dir = x.c_str();
    }


    find = FindFirstFileW(dir, &fd);

    do {
        if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // current file is a dir
            get_files(dir, ext, ret);
        else {
            LPCWSTR current_ext = PathFindExtensionW(fd.cFileName);
            if(ext.find(current_ext) != ext.end()) {
                ret << fd.cFileName << '\n';
            }
        }
    } while (FindNextFileW(find, &fd) != 0);

    FindClose(find);

    return 0;

}
auto get_files(_In_uuconst std::wstring root,//搜索的根目录
_在uuconst std::set&ext中,//要搜索的扩展名)
_Out_uuStd::wofstream&ret/*文件以将路径写入*/)->int
{
句柄查找=无效的句柄值;
//检查根路径
{
LPCWSTR root_path=root.c_str();
DWORD root_attrib=GetFileAttributesW(root_路径);
如果(root\u attrib==无效的\u文件\u属性)返回1;//root不存在
如果(!(根属性&文件属性目录))返回2;//根不是目录
}
LPCWSTR-dir;
WIN32_FIND_DATAW fd;
//迪尔康卡特
{
std::wstring x=root.c_str();
x、 附加(L“\\*”);
dir=x.c_str();
}
find=FindFirstFileW(dir和fd);
做{
if(fd.dwFileAttributes&FILE\u ATTRIBUTE\u DIRECTORY)//当前文件是目录
获取_文件(dir、ext、ret);
否则{
LPCWSTR current_ext=PathFindExtensionW(fd.cFileName);
if(ext.find(current_ext)!=ext.end()){
ret两个问题:

  • 键入
    std::set&ext
    LPCWSTR current\u ext
    之间的冲突。您可以使用
    std::set&ext
    解决E0304错误
  • 正如@Botje指出的,请注意
    std::wstring x
    的工作范围。您可以移除外部支架
  • 以下代码已为我成功编译。请检查:

    #include <shlwapi.h>
    #include <set>
    #include <fstream>
    
    auto get_files(_In_ const std::wstring root, // root dir of search
        _In_ const std::set<std::wstring> &ext, // extensions to search for)
        _Out_ std::wofstream &ret /* file to write paths to */) -> int
    {
        HANDLE find = INVALID_HANDLE_VALUE;
    
        // check root path
        {
            LPCWSTR root_path = root.c_str();
            DWORD root_attrib = GetFileAttributesW(root_path);
            if (root_attrib == INVALID_FILE_ATTRIBUTES) return 1; // root doesn't exist
            if (!(root_attrib & FILE_ATTRIBUTE_DIRECTORY)) return 2; // root isn't a directory
        }
    
        LPCWSTR dir;
        WIN32_FIND_DATAW fd;
    
        // dir concat
        std::wstring x = root.c_str();
        x.append(L"\\*");
        dir = x.c_str();
    
        find = FindFirstFileW(dir, &fd);
    
        do {
            if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // current file is a dir
                get_files(dir, ext, ret);
            else {
                LPCWSTR current_ext = PathFindExtensionW(fd.cFileName);
                if (ext.find(current_ext) != ext.end()) {
                    ret << fd.cFileName << '\n';
                }
            }
        } while (FindNextFileW(find, &fd) != 0);
    
        FindClose(find);
    
        return 0;
    }
    
    #包括
    #包括
    #包括
    自动获取\u文件(\u在\uConst std::wstring root中,//搜索的根目录
    _在uuconst std::set&ext中,//要搜索的扩展名)
    _Out_uuStd::wofstream&ret/*文件以将路径写入*/)->int
    {
    句柄查找=无效的句柄值;
    //检查根路径
    {
    LPCWSTR root_path=root.c_str();
    DWORD root_attrib=GetFileAttributesW(root_路径);
    如果(root\u attrib==无效的\u文件\u属性)返回1;//root不存在
    如果(!(根属性&文件属性目录))返回2;//根不是目录
    }
    LPCWSTR-dir;
    WIN32_FIND_DATAW fd;
    //迪尔康卡特
    std::wstring x=root.c_str();
    x、 附加(L“\\*”);
    dir=x.c_str();
    find=FindFirstFileW(dir和fd);
    做{
    if(fd.dwFileAttributes&FILE\u ATTRIBUTE\u DIRECTORY)//当前文件是目录
    获取_文件(dir、ext、ret);
    否则{
    LPCWSTR current_ext=PathFindExtensionW(fd.cFileName);
    if(ext.find(current_ext)!=ext.end()){
    
    ret第二个错误说明了一切:
    无法将参数1从“const wchar\u t*”转换为(的计算版本)std::string
    。还要注意,
    dir
    指向被破坏的
    std::wstring
    的内容。如果这样做是偶然的。您无法将
    std::string
    std::wstring
    进行比较。因此,请将
    std::wstring
    文件名转换为
    std::string
    ,或者使用de>std::set
    of
    std::wstring
    s。这是您的选择。除非您使用指向已删除的
    std::string
    的指针修复未定义的行为,否则请准备好处理随机、不可预测、神秘的错误。