簡體   English   中英

Boost Filesystem存在訪問沖突

[英]Boost Filesystem exists Access violation

我在boost::filesystem::exists(pathName)遇到問題。

當文件夾不存在時,該方法將按預期返回false 如果Folder存在,它將在Boost File 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:

這是一個示例,應該在帶有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;
}

有沒有更好的方法來檢查特定目錄是否存在?

您也可以在MFC中使用PathFileExists

正如MSDN所說:

確定文件系統對象(例如文件或文件夾)的路徑是否有效。

我已經多次使用此功能,沒有任何問題。 如本文所述,您必須鏈接外殼輕量級API才能使其正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM