std::experimental::filesystem::u8path
来自cppreference.com
| 在标头 <experimental/filesystem> 定义
|
||
| |
(1) | (文件系统 TS) |
| |
(2) | (文件系统 TS) |
从以 std::string,或者空终结多字节字符串,或者作为 [first, last) 迭代器对提供的 UTF-8 编码的 char 序列构造路径 p。
- 如果
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 字符序列转换为 UTF-32 编码的
std::u32string类型的临时字符串tmp,然后如同以path(tmp)构造新路径(在具有非 Unicode 多字节编码或单字节编码的文件系统的 POSIX 系统上会获得这种路径)。
参数
| source | - | UTF-8 编码的 std::string,指向空终止多字节字符串的指针,或具有 char 值类型并指向空终止多字节字符串的输入迭代器 |
| first, last | - | 一对老式输入迭代器 (LegacyInputIterator) ,指定 UTF-8 编码的字符序列 |
| 类型要求 | ||
-InputIt 必须满足老式输入迭代器 (LegacyInputIterator) 。
| ||
-InputIt 的值类型必须为 char。
|
返回值
将输入字符串转换从 UTF-8 转换为文件系统的原生字符编码,以之构造的路径。
异常
在发生底层 OS API 错误时可能抛出 filesystem_error,或若发生内存分配失败可能抛出 std::bad_alloc。
注解
在原生路径格式与通用路径格式不同的系统上(Windows 和 POSIX 系统都不是这种 OS 的例子),如果此函数的实参使用通用格式,则将其转换为原生格式。
示例
Run this code
#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");
// 原生字符串表示可以与 OS API 一起使用
std::ofstream(p) << "文件内容"; // 这里使用 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 << "\n窄多字节编码的文件名:"
<< p.string() << '\n';
std::wcerr.imbue(std::locale());
std::wcerr << "宽编码的文件名:"
<< p.wstring() << '\n';
fs::remove(p);
}
可能的输出:
文件内容
窄多字节编码的文件名:要らない.txt
宽编码的文件名:要らない.txt
参阅
| 表示一个路径 (类) |