close
名前空間
変種

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

提供: cppreference.com
 
 
 
 
<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num t-dcl-rev-notes t-since-cxx17 "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>
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 の場合、変換はシステム依存です。 一般的な POSIX システム (Linux など) が該当します。 それらのシステムではネイティブエンコーディングは UTF-8 で、 string() は変換を行いません。
  • path::value_typewchar_t の場合、変換は未規定です。 Windows が該当します。 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 <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

関連項目

文字列に変換された汎用パス名形式のパスを返します
(パブリックメンバ関数) [edit]