名前空間
変種
操作

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

From 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 <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

[編集] 関連項目

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