std::experimental::filesystem::path::c_str, std::experimental::filesystem::path::native, std::experimental::filesystem::path::operator string_type()
来自 cppreference.cn
< cpp | experimental | fs | path
const value_type* c_str() const; |
(1) | (文件系统 TS) |
const string_type& native() const; |
(2) | (文件系统 TS) |
operator string_type() const; |
(3) | (文件系统 TS) |
以 C 风格字符串的形式访问原生路径名。
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
[编辑] 参见
以原生路径名格式返回转换为字符串的路径 (公共成员函数) | |
以通用路径名格式返回转换为字符串的路径 (公共成员函数) |