名前空間
変種
操作

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

From cppreference.com
< cpp‎ | experimental‎ | fs‎ | path
 
 
 
 
 
const value_type* c_str() const;
(1) (filesystem TS)
const string_type& native() const;
(2) (filesystem TS)
operator string_type() const;
(3) (filesystem TS)

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

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

目次

[編集] パラメータ

(なし)

[編集] 戻り値

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

[編集] 例外

1,2)
noexcept 指定:  
noexcept
  

[編集] 注釈

変換関数 (3) は、std::basic_string ファイル名を受け入れる標準ファイルオープニング API(例: std::ifstream コンストラクタ)が、コードの変更なしにパス名を使用できるように提供されています。

fs::path p = "/tmp/text.txt";
std::ifstream f(p);

[編集]

#include <clocale>
#include <cstdio>
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::filesystem;
 
int main()
{
    std::setlocale(LC_ALL, "en_US.utf8");
    std::locale::global(std::locale("en_US.utf8"));
 
    fs::path p = fs::u8path(u8"要らない.txt");
 
    // native string representation can be used with OS APIs
    std::ofstream(p) << "File contents"; // this uses operator string()
    if (std::FILE* f = std::fopen(p.c_str(), "r"))
    {
        int ch;
        while ((ch=fgetc(f))!= EOF) putchar(ch);
        std::fclose(f);
    }
 
    // multibyte and wide representation can be used for output
    std::cout.imbue(std::locale());
    std::cout << "\nFile name in narrow multibyte encoding: "
              << p.string() << '\n';
 
    std::wcerr.imbue(std::locale());
    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

[編集] 関連項目

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