Namespaces
Variants

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

来自 zh.cppreference.net
 
 
实验性
技术规范
文件系统库 (文件系统 TS)
库基础 (库基础 TS)
库基础 2 (库基础 TS v2)
库基础 3 (库基础 TS v3)
并行扩展 (并行 TS)
并行扩展 2 (并行 TS v2)
并发扩展 (并发 TS)
并发扩展 2 (并发 TS v2)
概念 (概念 TS)
范围 (范围 TS)
反射 (反射 TS)
数学特殊函数 (特殊函数 TR)
实验性非 TS
模式匹配
线性代数
std::execution
契约
2D 图形
 
 
 
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

参见

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