gpt4 book ai didi

c++ - Boost 文件系统存在访问冲突

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:40 25 4
gpt4 key购买 nike

我对 boost::filesystem::exists(pathName) 有疑问。

当文件夹不存在时,该方法按预期返回 false。如果文件夹存在,它会在 Boost 文件 operations.cppmake_permissions(const path& p, DWORD attr) 方法中抛出异常。

这是文件中的代码

if (equal_string_ordinal_ic(ext.c_str(), L".exe")
|| equal_string_ordinal_ic(ext.c_str(), L".com")
|| equal_string_ordinal_ic(ext.c_str(), L".bat")
|| equal_string_ordinal_ic(ext.c_str(), L".cmd"))
prms |= fs::owner_exe | fs::group_exe | fs::others_exe;
return prms;

信息是:

Exception thrown at 0x00000000 in LineSync.exe: 0xC0000005: Access violation executing location 0x00000000.

这是在 if 语句中抛出的。我在 Windows 10 下的 MFC 应用程序中使用此方法。在 Windows 7 上,问题不存在。

如果我在 Windows 10 上创建示例 Windows 控制台应用程序,问题也没有发生。

此代码不适用于 MFC 项目,但适用于 Windows 10 控制台应用程序。

boost::filesystem::path pathDir(L"logs");
if (!boost::filesystem::exists(pathDir)) {
boost::filesystem::create_directory(pathDir);
}

更改为 boost::filesystem::is_directory(pathDir) 也会导致 MFC 项目发生异常

MFC 和 Windows 10 有问题吗?或者是否有更好的方法来检查特定目录是否存在?

编辑 2:

这是一个在 Windows 上使用 Boost 1.66 时应该会导致错误的示例

bool dummy = boost::filesystem::exists("logs");

class Demo {
public:
~Demo() {
std::cout << boost::filesystem::path("logs").string() << std::endl;
}
};

Demo d;

int main()
{
Demo();
system("PAUSE");
return 0;
}

最佳答案

Is there a better way to check if a specific directory exists?

您还可以使用 PathFileExists使用 MFC。

正如 MSDN 所说:

Determines whether a path to a file system object such as a file or folder is valid.

我已经多次使用此功能,没有任何问题。如文章中所述,您必须链接到 shell 轻量级 API 才能使其正常工作。

关于c++ - Boost 文件系统存在访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48766009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com