x3fang_c 2024-07-29 08:17 采纳率: 8.7%
浏览 4

C++ filesystem error

我正在尝试用 C++17 中的 filesystem 库遍历所有文件
但我遇到了问题:


#include <filesystem>
#include <iostream>
#include <string.h>
#include <comdef.h>
#include <string>
#include <windows.h>
using std::string;
using std::wstring; 
std::wstring String2Wstring(std::string wstr)
{
    std::wstring res;
    int len = MultiByteToWideChar(CP_ACP, 0, wstr.c_str(), wstr.size(), nullptr, 0);
    if( len < 0 ){
        return res;
    }
    wchar_t* buffer = new wchar_t[len + 1];
    if( buffer == nullptr){
        return res;
    }
    MultiByteToWideChar(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len);
    buffer[len] = '\0';
    res.append(buffer);
    delete[] buffer;
    return res;
}
void FindFileEx_Cpp17(std::wstring pagthd) {
    
    string paths,name;
    try{
        auto er=std::error_code();
    for (auto &v:std::filesystem::recursive_directory_iterator(pagthd,std::filesystem::directory_options::skip_permission_denied,er))
    {
        if(er)
        {
            continue;
        }
        paths=v.path().string();
        name=v.path().filename().string();
        std::filesystem::path pathByfsp(paths);
        if(v.is_regular_file())
        {
            std::cout<<paths<<std::endl;
        }
        else{
            FindFileEx_Cpp17(pathByfsp);
        }
    }}
    catch (std::exception& e)
    {
        std::cout<<e.what()<<std::endl;
    }
}
bool getstart() {
    DWORD bufferLength = GetLogicalDriveStrings(0, nullptr);
    if (bufferLength == 0) {
        std::wcerr << L"Failed to get logical drive strings length." << std::endl;
        return false;
    }
    
    std::vector<wchar_t> buffer(bufferLength);
    DWORD result = GetLogicalDriveStringsW(bufferLength, buffer.data());
    if (result == 0 || result > bufferLength) {
        std::wcerr << L"Failed to get logical drive strings." << std::endl;
        return false;
    }
    
    wchar_t* drive = buffer.data();
    while (*drive) {
        std::wstring temp=drive; 
        std::wcout<<temp<<std::endl;
        FindFileEx_Cpp17(temp);
        drive += wcslen(drive) + 1;
    }
    return true;
}
int main()
{
    system("chcp 65001 > nul");
    getstart();
}

当它遍历到 C:\Documents and Settings 目录时抛出了一个问题:filesystem error: cannot increment recursive directory iterator: Invalid argument
我该怎么做

  • 写回答

2条回答 默认 最新

  • 何事_ 2024-07-29 08:17
    关注

    引自免费微信小程序:皆我百晓生

    首先,你需要明确错误“filesystem error: cannot increment recursive directory iterator: Invalid argument”表示的是当你试图在遍历过程中增加一个递归目录迭代器时遇到的问题。在某些文件系统操作中,当某些特定的路径不能被正确读取或无法打开时,会发生这种错误。在这个特定情况下,你的程序可能遇到了权限问题或者文件系统问题。对于这个问题,我有一些建议供你参考:

    1. 检查权限问题:确认你的程序有足够的权限访问所有的目录和文件。有些系统为了保护数据安全会对访问文件系统进行限制。你需要确认你的程序运行在合适的权限下。此外,使用Windows特性上的用户帐户控制 (UAC) 或更高权限的帐户运行你的程序可能有助于解决这个问题。
    2. 处理权限拒绝的情况:在你的代码中,你已经使用了 std::filesystem::directory_options::skip_permission_denied 选项来跳过权限被拒绝的情况。然而,你还需要处理这个选项触发时的其他情况,例如在 std::filesystem::directory_iterator 的错误处理中可能需要增加更多的检查逻辑。比如你可以在 try-catch 块中对错误进行检查和处理,对于 std::filesystem::directory_iterator 无法处理的情况,可以选择跳过或输出错误信息。
    3. 修改遍历方式:对于某些文件系统(尤其是嵌入式文件系统或非标准的文件系统),直接进行深度遍历可能会有问题。尝试将遍历逻辑更改为首先遍历所有顶层目录,然后再递归遍历子目录的方式可能会解决这个问题。这可能需要你修改 FindFileEx_Cpp17 函数中的遍历逻辑。
    4. 检查路径问题:确保你的代码正确处理了所有可能的路径问题。例如,某些路径可能包含特殊字符或格式问题,这可能导致文件系统库无法正确解析这些路径。尝试在你的代码中增加对路径的验证和清理逻辑。

    为了更准确地解决问题,你可能需要对代码进行调试,看看具体的错误是在哪一步发生的,并根据具体的情况进行修复。如果你能够提供更多关于错误信息的细节,我可能会提供更具体的帮助。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 7月29日

悬赏问题

  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开