std::filesystem::path:: string, std::filesystem::path:: wstring, std::filesystem::path:: u8string, std::filesystem::path:: u16string, std::filesystem::path:: u32string
|
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 且转换方法未作规定。
目录 |
参数
(无)
返回值
以原生路径名格式表示的内部路径名,已转换为指定的字符串类型。
异常
可能抛出实现定义的异常。
示例
#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"; // 原生字符串表示可用于操作系统API 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); } // 多字节和宽字符表示可用于输出 std::cout << "\nFile name in narrow multibyte encoding: " << p.string() << '\n'; // wstring() 在 stdlibc++ 中会抛出异常(基于 gcc-12.1.0),参见: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95048 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102839 // 在更新的 gcc-12.2.1 (2023/02/01) 和 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
参见
|
返回转换为字符串的通用路径名格式的路径
(公开成员函数) |