std::experimental::filesystem::u8path
來自 cppreference.com
< cpp | experimental | fs | path
定義於標頭檔案 <experimental/filesystem> |
||
template< class Source > path u8path( const Source& source ); |
(1) | (filesystem TS) |
template< class InputIt > path u8path( InputIt first, InputIt last ); |
(2) | (filesystem TS) |
從 UTF-8 編碼的 char 序列構造路徑 p
,該序列可以是 std::string、以 null 結尾的多位元組字串或 [first, last) 迭代器對。
- 如果
path::value_type
是 char 且本地編碼是 UTF-8,則直接構造路徑,如同透過 path(source) 或 path(first, last)。注意:這是使用 Unicode 的 POSIX 系統的典型情況,例如 Linux。 - 否則,如果
path::value_type
是 wchar_t 且本地編碼是 UTF-16(這是 Windows 上的情況),或者如果path::value_type
是 char16_t(保證本地編碼為 UTF-16)或 char32_t(保證本地編碼為 UTF-32),則首先將 UTF-8 字元序列轉換為path::string_type
型別的臨時字串tmp
,然後構造新路徑,如同透過 path(tmp)。 - 否則(對於非 UTF-8 窄字元編碼和非 UTF-16 wchar_t),首先將 UTF-8 字元序列轉換為 std::u32string 型別的臨時 UTF-32 編碼字串
tmp
,然後構造新路徑,如同透過 path(tmp)(在具有非 Unicode 多位元組或單位元組編碼檔案系統的 POSIX 系統上採用此路徑)。
目錄 |
[編輯] 引數
source | - | UTF-8 編碼的 std::string、指向以 null 結尾的多位元組字串的指標,或者指向以 null 結尾的多位元組字串的 char 值型別的輸入迭代器 |
first, last | - | 一對指定了 UTF-8 編碼字元序列的遺留輸入迭代器 (LegacyInputIterator) |
型別要求 | ||
-InputIt 必須滿足 LegacyInputIterator 的要求。 | ||
-InputIt 的值型別必須是 char。 |
[編輯] 返回值
從輸入字串構造的路徑,經過從 UTF-8 到檔案系統原生字元編碼的轉換。
[編輯] 異常
如果底層作業系統 API 錯誤或記憶體分配失敗,可能會丟擲 filesystem_error 或 std::bad_alloc。
[編輯] 注意
在原生路徑格式與通用路徑格式不同的系統上(Windows 或 POSIX 系統都不是此類作業系統的示例),如果此函式的引數使用通用格式,它將被轉換為原生格式。
[編輯] 示例
執行此程式碼
#include <clocale> #include <cstdio> #include <experimental/filesystem> #include <fstream> #include <iostream> namespace fs = std::experimental::filesystem; int main() { std::setlocale(LC_ALL, "en_US.utf8"); std::locale::global(std::locale("en_US.utf8")); fs::path p = fs::u8path(u8"要らない.txt"); // native string representation can be used with OS APIs std::ofstream(p) << "File contents"; // this uses operator string() if (std::FILE* f = std::fopen(p.c_str(), "r")) { int ch; while ((ch=fgetc(f))!= EOF) putchar(ch); std::fclose(f); } // multibyte and wide representation can be used for output std::cout.imbue(std::locale()); std::cout << "\nFile name in narrow multibyte encoding: " << p.string() << '\n'; std::wcerr.imbue(std::locale()); std::wcerr << "File name in wide encoding: " << p.wstring() << '\n'; fs::remove(p); }
可能的輸出
File contents File name in narrow multibyte encoding: 要らない.txt File name in wide encoding: 要らない.txt
[編輯] 參閱
表示一個路徑 (類) |