std::experimental::filesystem:: u8path
|
定义于头文件
<experimental/filesystem>
|
||
|
template
<
class
Source
>
path u8path ( const Source & source ) ; |
(1) | (文件系统 TS) |
|
template
<
class
InputIt
>
path u8path ( InputIt first, InputIt last ) ; |
(2) | (文件系统 TS) |
构造一个路径
p
,该路径来自以 UTF-8 编码的
char
序列,可通过以下方式提供:
std::string
,或以空字符结尾的多字节字符串,或通过 [
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 ,指向以空字符结尾的多字节字符串的指针,或指向以空字符结尾的多字节字符串的char值类型输入迭代器 |
| first, last | - | 指定UTF-8编码字符序列的 LegacyInputIterator s 对 |
| 类型要求 | ||
-
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"); // 原生字符串表示可用于操作系统API std::ofstream(p) << "File contents"; // 此处使用operator string() if (std::FILE* f = std::fopen(p.c_str(), "r")) { int ch; while ((ch=fgetc(f))!= EOF) putchar(ch); std::fclose(f); } // 多字节和宽字符表示可用于输出 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
参见
|
表示路径
(类) |