std::experimental::filesystem::path::string,wstring,u8string,...
来自 zh.cppreference.net
template< class CharT, class Traits = std::char_traits<CharT>,
class Alloc = std::allocator<CharT> >
std::basic_string<CharT,Traits,Alloc>
string( const Alloc& a = Alloc() ) const;
|
(1) | (文件系统 TS) |
std::string string() const;
|
(2) | (文件系统 TS) |
std::wstring wstring() const;
|
(文件系统 TS) | |
std::string u8string() const;
|
(文件系统 TS) | |
std::u16string u16string() const;
|
(文件系统 TS) | |
std::u32string u32string() const;
|
(文件系统 TS) | |
返回以本地路径名格式表示的内部路径名,转换为特定的字符串类型。任何转换(如果有)在待办事项中指定。
1) 所有内存分配由
a执行。2) 在
u8string()的情况下,编码始终为 UTF-8。返回值
以原生路径名格式表示的内部路径名,已转换为指定的字符串类型。
异常
可能抛出实现定义的异常。
示例
运行此代码
#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"); // 原生字符串表示可用于操作系统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
参见
|
返回以通用路径名格式转换的字符串路径
(公开成员函数) |