Namespaces
Variants

std::experimental::filesystem::path:: string,wstring,u8string,...

From 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)
(2) (文件系统 TS)
std:: string string ( ) const ;
std:: wstring wstring ( ) const ;
std:: string u8string ( ) const ;
std:: u16string u16string ( ) const ;
std:: u32string u32string ( ) const ;

返回以原生路径名格式表示的内部路径名,并转换为特定字符串类型。如有转换需求,具体说明详见 待办事项

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

参见

返回以通用路径名格式转换的字符串路径
(公开成员函数)