std::filesystem::u8path - cppreference.com
提供: cppreference.com
<tbody> </tbody>
| ヘッダ |
||
|
|
(1) | (C++17以上) (C++20で非推奨) |
|
|
(2) | (C++17以上) (C++20で非推奨) |
std::string、std::string_view、ヌル終端マルチバイト文字列、またはイテレータの組 [first, last) として提供される UTF-8 エンコードされた char シーケンスからパスを構築します。
path::value_typeがcharでネイティブエンコーディングが UTF-8 の場合、path(source)またはpath(first, last)で行われたかのように、パスを直接構築します。 ノート: これは Linux のような Unicode を使用する POSIX システムの一般的な状況です。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、std::string_view、ヌル終端文字列を指すポインタ、またはヌル終端文字列を指す文字値型の入力イテレータ |
| first, last | - | UTF-8 エンコードされた文字シーケンスを表す一組の LegacyInputIterator |
| 型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-InputIt の値型は char でなければなりません。
| ||
戻り値
UTF-8 からファイルシステムのネイティブ文字エンコーディングに変換された後の入力文字列から構築されたパス。
例外
メモリ確保に失敗した場合は std::bad_alloc を投げる可能性があります。
ノート
ネイティブパス形式が汎用パス形式と異なるシステム (Windows や POSIX システムはいずれもそういった OS の例ではありません) では、この関数の引数に汎用形式を使用すると、ネイティブ形式に変換されます。
例
#include <cstdio> #ifdef _MSC_VER #include <io.h> #include <fcntl.h> #else #include <locale> #include <clocale> #endif #include <fstream> #include <iostream> #include <filesystem> namespace fs = std::filesystem; int main() { #ifdef _MSC_VER _setmode(_fileno(stderr), _O_WTEXT); #else std::setlocale(LC_ALL, ""); std::locale::global(std::locale("")); std::cout.imbue(std::locale()); std::wcerr.imbue(std::locale()); #endif fs::path p = fs::u8path(u8"要らない.txt"); std::ofstream(p) << "File contents"; // LWG2676 の前では、 string_type が wstring // である MSVC においては、 operator string_type() // の使用は非標準の拡張によってのみ動作します。 // LWG2676 の後では、 fstream の新しいコンストラクタ // が使用されます。 // ネイティブ文字列表現は OS の API で使用できます。 if (std::FILE* f = #ifdef _MSC_VER _wfopen(p.c_str(), L"r") #else std::fopen(p.c_str(), "r") #endif ) { int ch; while((ch=fgetc(f))!= EOF) putchar(ch); std::fclose(f); } // マルチバイト表現とワイド表現は出力のために使用できます。 std::cout << "\nFile name in narrow multibyte encoding: " << p.string() << '\n'; 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