std::filesystem::path::make_preferred
來自 cppreference.com
< cpp | filesystem | path
path& make_preferred(); |
(C++17 起) | |
將路徑中所有目錄分隔符轉換為偏好的目錄分隔符。
例如,在 Windows 上,其中 \ 是偏好的分隔符,路徑 foo/bar 將被轉換為 foo\bar。
目錄 |
[編輯] 引數
(無)
[編輯] 返回值
*this
[編輯] 異常
可能丟擲實現定義的異常。
[編輯] 示例
Windows 可以使用 / 作為分隔符,但偏好使用 \,所以 make_preferred 會將正斜槓轉換為反斜槓。另一方面,POSIX 不使用 \ 作為分隔符,因為反斜槓是合法的檔名字元——Windows 路徑在 POSIX 上實際上指向一個名為 "a\\b\\c" 的檔案。因此,“分隔符”不會被轉換。
執行此程式碼
#include <filesystem> #include <iostream> int main() { std::filesystem::path windows_path("a\\b\\c"), posix_path("a/b/c"); std::cout << "Windows path: " << windows_path << " -> " << windows_path.make_preferred() << '\n' << "POSIX path: " << posix_path << " -> " << posix_path.make_preferred() << '\n'; }
輸出
// on Windows Windows path: "a\\b\\c" -> "a\\b\\c" POSIX path: "a/b/c" -> "a\\b\\c" // on POSIX Windows path: "a\\b\\c" -> "a\\b\\c" POSIX path: "a/b/c" -> "a/b/c"
[編輯] 參閱
constexpr value_type preferred_separator [靜態] |
除了可移植的 / 之外,還可以使用的備選目錄分隔符。在 Windows 上,這是反斜槓字元 \。在 POSIX 上,這與可移植的分隔符相同,都是正斜槓 /。 (公開靜態成員常量) |