std::experimental::filesystem::path::c_str, std::experimental::filesystem::path::native, std::experimental::filesystem::path::operator string_type()
來自 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) 透過值返回路徑名的本機字串表示。
目錄 |
[編輯] 引數
(無)
[編輯] 返回值
路徑名的本機字串表示,使用本機語法、本機字元型別和本機字元編碼。此字串適用於與作業系統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
[編輯] 參閱
返回轉換為字串的原生路徑名格式的路徑 (公共成員函式) | |
返回轉換為字串的通用路徑名格式的路徑 (公共成員函式) |