名稱空間
變體
操作

std::experimental::filesystem::u8path

來自 cppreference.com
< cpp‎ | experimental‎ | fs‎ | path
 
 
實驗性
技術規範
檔案系統庫 (檔案系統 TS)
庫基礎 (庫基礎 TS)
庫基礎 2 (庫基礎 TS v2)
庫基礎 3 (庫基礎 TS v3)
並行性擴充套件 (並行性 TS)
並行性擴充套件 2 (並行性 TS v2)
併發性擴充套件 (併發性 TS)
併發擴充套件 2 (concurrency TS v2)
概念 (概念 TS)
範圍 (範圍 TS)
反射 (反射 TS)
數學特殊函式 (特殊函式 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、以 null 結尾的多位元組字串或 [first, last) 迭代器對。

  • 如果 path::value_typechar 且本地編碼是 UTF-8,則直接構造路徑,如同透過 path(source)path(first, last)。注意:這是使用 Unicode 的 POSIX 系統的典型情況,例如 Linux。
  • 否則,如果 path::value_typewchar_t 且本地編碼是 UTF-16(這是 Windows 上的情況),或者如果 path::value_typechar16_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、指向以 null 結尾的多位元組字串的指標,或者指向以 null 結尾的多位元組字串的 char 值型別的輸入迭代器
first, last - 一對指定了 UTF-8 編碼字元序列的遺留輸入迭代器 (LegacyInputIterator)
型別要求
-
InputIt 必須滿足 LegacyInputIterator 的要求。
-
InputIt 的值型別必須是 char

[編輯] 返回值

從輸入字串構造的路徑,經過從 UTF-8 到檔案系統原生字元編碼的轉換。

[編輯] 異常

如果底層作業系統 API 錯誤或記憶體分配失敗,可能會丟擲 filesystem_errorstd::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

[編輯] 參閱

表示一個路徑
(類) [編輯]