命名空间
变体
操作

std::experimental::filesystem::u8path

来自 cppreference.cn
< cpp‎ | experimental‎ | fs‎ | path
 
 
实验性
技术规范
文件系统库 (filesystem TS)
库基础 (library fundamentals TS)
库基础 2 (library fundamentals TS v2)
库基础 3 (library fundamentals TS v3)
并行性扩展 (parallelism TS)
并行性扩展 2 (parallelism TS v2)
并发性扩展 (concurrency TS)
并发性扩展 2 (concurrency TS v2)
概念 (concepts TS)
范围 (ranges TS)
反射 (reflection TS)
数学特殊函数 (special functions TR)
实验性非TS
模式匹配
线性代数
std::execution
契约
2D图形
 
 
 
定义于头文件 <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、空终止多字节字符串或 [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 对
类型要求
-
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

[编辑] 参见

表示路径
(类) [编辑]