std::filesystem::path::string, std::filesystem::path::wstring, std::filesystem::path::u8string, std::filesystem::path::u16string, std::filesystem::path::u32string
來自 cppreference.com
< cpp | filesystem | path
template< class CharT, class Traits = std::char_traits<CharT>, class Alloc = std::allocator<CharT> > |
(1) | (C++17 起) |
(2) | (C++17 起) | |
std::string string() const; |
||
std::wstring wstring() const; |
||
std::u16string u16string() const; |
||
std::u32string u32string() const; |
||
(3) | ||
std::string u8string() const; |
(C++17 起) (C++20 前) |
|
std::u8string u8string() const; |
(C++20 起) | |
返回內部路徑名,以原生路徑名格式轉換為特定字串型別。轉換(如果有)如下進行:
- 如果
path::value_type
是 char,則轉換(如果有)是系統相關的。這在典型的 POSIX 系統(例如 Linux)中是這種情況,其中原生編碼是 UTF-8,並且string()
不執行任何轉換。 - 否則,如果
path::value_type
是 wchar_t,則轉換(如果有)是未指定的。這在 Windows 中是這種情況,其中 wchar_t 是 16 位,原生編碼是 UTF-16。 - 否則,如果
path::value_type
是 char16_t,則原生編碼是 UTF-16,轉換方法未指定。 - 否則,如果
path::value_type
是 char32_t,則原生編碼是 UTF-32,轉換方法未指定。 - 否則,如果
path::value_type
是 char8_t,則原生編碼是 UTF-8,轉換方法未指定。
1) 所有記憶體分配都由 a 執行。
3) 在 u8string() 的情況下,結果編碼始終是 UTF-8。
目錄 |
[編輯] 引數
(無)
[編輯] 返回值
內部路徑名,以原生路徑名格式轉換為指定的字串型別。
[編輯] 異常
可能丟擲實現定義的異常。
[編輯] 示例
執行此程式碼
#include <clocale> #include <cstdio> #include <filesystem> #include <fstream> #include <iostream> #include <locale> int main() { const char* const localeName = "ja_JP.utf-8"; std::setlocale(LC_ALL, localeName); std::locale::global(std::locale(localeName)); const std::filesystem::path p(u8"要らない.txt"); std::ofstream(p) << "File contents"; // native string representation can be used with OS APIs if (std::FILE* const f = std::fopen(p.string().c_str(), "r")) { for (int ch; (ch = std::fgetc(f)) != EOF;) std::putchar(ch); std::fclose(f); } // multibyte and wide representation can be used for output std::cout << "\nFile name in narrow multibyte encoding: " << p.string() << '\n'; // wstring() will throw in stdlibc++ (as per gcc-12.1.0), see: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95048 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102839 // works with more recent gcc-12.2.1 (2023/02/01) and clang-10+ std::wcout << "File name in wide encoding: " << p.wstring() << '\n'; std::filesystem::remove(p); }
可能的輸出
File contents File name in narrow multibyte encoding: 要らない.txt File name in wide encoding: 要らない.txt
[編輯] 參閱
返回轉換為字串的通用路徑名格式的路徑 (公共成員函式) |