名前空間
変種
操作

std::filesystem::u8path

From cppreference.com
< cpp‎ | filesystem‎ | path
 
 
 
 
ヘッダー <filesystem> で定義
template< class Source >
std::filesystem::path u8path( const Source& source );
(1) (C++17以降)
(C++20で非推奨)
template< class InputIt >
std::filesystem::path u8path( InputIt first, InputIt last );
(2) (C++17以降)
(C++20で非推奨)

UTF-8エンコードされたcharまたはchar8_tのシーケンスからパスpを構築します。これは、std::stringstd::string_view、ヌル終端マルチバイト文字列、または[first, last)イテレータペアとして提供されます。

  • path::value_typecharで、ネイティブエンコーディングがUTF-8の場合、パスはpath(source)またはpath(first, last)のように直接構築されます。注:これは、LinuxのようなUnicodeを使用するPOSIXシステムの典型的な状況です。
  • それ以外の場合、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::stringstd::string_view、ヌル終端マルチバイト文字列へのポインタ、またはヌル終端マルチバイト文字列を指すchar値型の入力イテレータ
first, last - UTF-8エンコードされた文字シーケンスを指定する、LegacyInputIteratorのペア
型要件
-
InputItLegacyInputIterator の要件を満たす必要があります。
-
SourceまたはInputItの値型は、charまたはchar8_tである必要があります。

[編集] 戻り値

UTF-8からファイルシステムのネイティブ文字エンコーディングへの変換後、入力文字列から構築されたパス。

[編集] 例外

メモリ割り当てに失敗した場合、std::bad_allocをスローする可能性があります。

[編集] 注記

ネイティブパス形式が汎用パス形式と異なるシステム(WindowsまたはPOSIXシステムはこのようなOSの例ではない)では、この関数への引数が汎用形式を使用している場合、ネイティブ形式に変換されます。

[編集]

#include <cstdio>
#ifdef _MSC_VER
#include <fcntl.h>
#include <io.h>
#else
#include <clocale>
#include <locale>
#endif
#include <filesystem>
#include <fstream>
 
int main()
{
#ifdef _MSC_VER
    _setmode(_fileno(stderr), _O_WTEXT);
#else
    std::setlocale(LC_ALL, "");
    std::locale::global(std::locale(""));
#endif
 
    std::filesystem::path p(u8"要らない.txt");
    std::ofstream(p) << "File contents"; // Prior to LWG2676 uses operator string_type()
                                         // on MSVC, where string_type is wstring, only
                                         // works due to non-standard extension.
                                         // Post-LWG2676 uses new fstream constructors
 
    // Native string representation can be used with OS-specific APIs
#ifdef _MSC_VER
    if (std::FILE* f = _wfopen(p.c_str(), L"r"))
#else
    if (std::FILE* f = std::fopen(p.c_str(), "r"))
#endif
    {
        for (int ch; (ch = fgetc(f)) != EOF; std::putchar(ch))
        {}
        std::fclose(f);
    }
 
    std::filesystem::remove(p);
}

実行結果の例

File contents

[編集] 関連項目

(C++17)
パスを表現する
(クラス) [編集]
English 日本語 中文(简体) 中文(繁體)