std::experimental::filesystem::path:: c_str, std::experimental::filesystem::path:: native, std::experimental::filesystem::path:: operator string_type()
From cppreference.net
<
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"); // ネイティブ文字列表現はOS APIで使用可能 std::ofstream(p) << "File contents"; // これはoperator string()を使用 if (std::FILE* f = std::fopen(p.c_str(), "r")) { int ch; while ((ch=fgetc(f))!= EOF) putchar(ch); std::fclose(f); } // マルチバイトおよびワイド表現は出力に使用可能 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
関連項目
|
ネイティブパス名形式でパスを文字列に変換して返す
(公開メンバ関数) |
|
|
ジェネリックパス名形式でパスを文字列に変換して返す
(公開メンバ関数) |