名前空間
変種
操作

std::filesystem::path::c_str, std::filesystem::path::native, std::filesystem::path::operator string_type()

提供: cppreference.com
< cpp‎ | filesystem‎ | path
 
 
 
 
const value_type* c_str() const noexcept;
(1) (C++17以上)
const string_type& native() const noexcept;
(2) (C++17以上)
operator string_type() const;
(3) (C++17以上)

文字列としてのネイティブパス名にアクセスします。

1) native().c_str() と同等です。
2) パス名のネイティブ形式の表現を参照で返します。
3) パス名のネイティブ形式の表現を値で返します。

目次

[編集] 引数

(なし)

[編集] 戻り値

ネイティブな構文を使用し、ネイティブな文字型を使用し、ネイティブな文字エンコーディングを使用する、パス名のネイティブ文字列表現。 この文字列は OS の API で使用するのに適しています。

[編集] ノート

変���関数 (3) は、std::basic_string のファイル名を取る API でコードを変更せずにパス名を使用できるようにするために、提供されています。

[編集]

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