首页 > 解决方案 > 为什么列出目录中所有文件的代码会崩溃?

问题描述

它不会立即崩溃,for循环列出了一些文件路径,然后我得到了这个异常:

testpath.exe 中 0x76F05608 处未处理的异常:Microsoft C++ 异常:内存位置 0x0044F8A8 处的 std::filesystem::filesystem_error。

CSIDL_DESKTOP但是,如果它不是,它不会崩溃CSIDL_PROGRAM_FILESX86

#include <iostream>
#include <filesystem>
#include <shlobj.h>

#pragma comment(lib, "shell32.lib")

namespace fs = std::filesystem;

int main()
{
    CHAR programx86[MAX_PATH];
    HRESULT result = SHGetFolderPath(NULL, CSIDL_PROGRAM_FILESX86, NULL, SHGFP_TYPE_CURRENT, programx86);

    for (auto& p : fs::recursive_directory_iterator(programx86))
    {
        std::cout << p.path().u8string() << std::endl;
    }
}

标签: c++crashfilesystems

解决方案


推荐阅读