【问题标题】:Boost Filesystem exists Access violationBoost Filesystem 存在访问冲突
【发布时间】:2018-02-13 11:41:09
【问题描述】:

boost::filesystem::exists(pathName) 有问题。

当文件夹不存在时,该方法按预期返回false。 如果文件夹存在,它将在 Boost 文件 operations.cpp 的 make_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:

这是一个示例,它会在使用 Boost 1.66 的 Windows 上导致错误

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

【问题讨论】:

标签: c++ boost mfc


【解决方案1】:

有没有更好的方法来检查特定目录是否存在?

您还可以将PathFileExists 与 MFC 一起使用。

正如 MSDN 所说:

确定文件系统对象(如文件或文件夹)的路径是否有效。

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

【讨论】:

  • PathFileExists() 参考文献提到了 MAX_PATH 限制。该参考资料没有说明是否可以通过使用“\\?\”前缀或选择移除MAX_PATH 限制而不使用“\\?\”前缀来规避此问题,该前缀自 Windows 10 版本 1607 起可用。 GetFileAttributes() 的引用,也可用于检查文件是否存在,明确提到了这些可能性。
  • @zett42 很有趣。我不知道那个答案!
猜你喜欢
  • 2017-09-06
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 2017-07-07
  • 1970-01-01
相关资源
最近更新 更多