std::experimental::filesystem::path::string,wstring,u8string,...
来自 cppreference.cn
< cpp | experimental | fs | path
template< class CharT, class Traits = std::char_traits<CharT>, class Alloc = std::allocator<CharT> > |
(1) | (filesystem TS) |
(2) | (filesystem TS) | |
std::string string() const; |
||
std::wstring wstring() const; |
||
std::string u8string() const; |
||
std::u16string u16string() const; |
||
std::u32string u32string() const; |
||
以特定字符串类型返回本机路径名格式的内部路径名。如有转换,将在todo中指定。
1) 所有内存分配均由 a 执行。
2) 在
u8string()
的情况下,编码始终为 UTF-8。目录 |
[edit] 参数
(无)
[edit] 返回值
本机路径名格式的内部路径名,转换为指定的字符串类型。
[edit] 异常
可能抛出实现定义的异常。
[edit] 示例
运行此代码
#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
[edit] 参见
返回转换为字符串的通用路径名格式的路径 (公共成员函数) |