名前空間
変種
操作

std::filesystem::path::string, std::filesystem::path::wstring, std::filesystem::path::u8string, std::filesystem::path::u16string, std::filesystem::path::u32string

From cppreference.com
< cpp‎ | filesystem‎ | path
 
 
 
 
template< class CharT, class Traits = std::char_traits<CharT>,

          class Alloc = std::allocator<CharT> >
std::basic_string<CharT,Traits,Alloc>

    string( const Alloc& a = Alloc() ) const;
(1) (C++17以降)
(2) (C++17以降)
std::string string() const;
std::wstring wstring() const;
std::u16string u16string() const;
std::u32string u32string() const;
(3)
std::string u8string() const;
(C++17以降)
(C++20まで)
std::u8string u8string() const;
(C++20以降)

ネイティブパス名形式の内部パス名を、指定された文字列型に変換して返します。変換は、該当する場合、以下のように行われます。

  • path::value_typechar の場合、変換(該当する場合)はシステム依存です。これは、ネイティブエンコーディングがUTF-8であり、string() が変換を行わない典型的なPOSIXシステム(Linuxなど)の場合に該当します。
  • それ以外の場合、path::value_typewchar_t の場合、変換(該当する場合)は未指定です。これは、Windowsの場合に該当し、wchar_t は16ビットであり、ネイティブエンコーディングはUTF-16です。
  • それ以外の場合、path::value_typechar16_t の場合、ネイティブエンコーディングはUTF-16であり、変換方法は未指定です。
  • それ以外の場合、path::value_typechar32_t の場合、ネイティブエンコーディングはUTF-32であり、変換方法は未指定です。
  • それ以外の場合、path::value_typechar8_t の場合、ネイティブエンコーディングはUTF-8であり、変換方法は未指定です。
1) すべてのメモリ割り当ては a によって行われます。
3) u8string() の場合、結果のエンコーディングは常にUTF-8です。

目次

[編集] パラメータ

(なし)

[編集] 戻り値

ネイティブパス名形式の内部パス名を、指定された文字列型に変換したもの。

[編集] 例外

実装定義の例外をスローする場合があります。

[編集]

#include <clocale>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <locale>
 
int main()
{
    const char* const localeName = "ja_JP.utf-8";
    std::setlocale(LC_ALL, localeName);
    std::locale::global(std::locale(localeName));
 
    const std::filesystem::path p(u8"要らない.txt");
    std::ofstream(p) << "File contents";
 
    // native string representation can be used with OS APIs
    if (std::FILE* const f = std::fopen(p.string().c_str(), "r"))
    {
        for (int ch; (ch = std::fgetc(f)) != EOF;)
            std::putchar(ch);
 
        std::fclose(f);
    }
 
    // multibyte and wide representation can be used for output
    std::cout << "\nFile name in narrow multibyte encoding: " << p.string() << '\n';
 
    // wstring() will throw in stdlibc++ (as per gcc-12.1.0), see:
    // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95048
    // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102839
    // works with more recent gcc-12.2.1 (2023/02/01) and clang-10+
    std::wcout << "File name in wide encoding: " << p.wstring() << '\n';
 
    std::filesystem::remove(p);
}

実行結果の例

File contents
File name in narrow multibyte encoding: 要らない.txt
File name in wide encoding: 要らない.txt

[編集] 関連項目

パスをジェネリックパス名形式から文字列に変換して返します。
(public member function) [編集]
English 日本語 中文(简体) 中文(繁體)