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 <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
[編輯] 參閱
返回轉換為字串的原生路徑名格式的路徑 (公開成員函式) | |
返回轉換為字串的通用路徑名格式的路徑 (公開成員函式) |