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

< cpp‎ | experimental‎ | fs‎ | path
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;

返回原生路径名格式的内部路径名,转换为指定的字符串类型。如果进行转换,则按 todo 进行。

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");
 
    // 原生字符串表示可以与 OS API 一起使用
    std::ofstream(p) << "文件内容"; // 这里使用 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 << "\n窄多字节编码的文件名:"
              << p.string() << '\n';
 
    std::wcerr.imbue(std::locale());
    std::wcerr << "宽编码的文件名:"
               << p.wstring() << '\n';
 
    fs::remove(p);
}

可能的输出:

文件内容
窄多字节编码的文件名:要らない.txt
宽编码的文件名:要らない.txt

参阅

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