Namespaces
Variants

std::experimental::filesystem::path:: c_str, std::experimental::filesystem::path:: native, std::experimental::filesystem::path:: operator string_type()

From cppreference.net
const value_type * c_str ( ) const ;
(1) (filesystem TS)
const string_type & native ( ) const ;
(2) (filesystem TS)
operator string_type ( ) const ;
(3) (filesystem TS)

以字符串形式访问原生路径名称。

1) 等价于 native ( ) . c_str ( )
2) 通过引用返回路径名的原生字符串表示。
3) 以值的形式返回路径名的原生字符串表示。

目录

参数

(无)

返回值

路径名的原生字符串表示,使用原生语法、原生字符类型和原生字符编码。该字符串适用于与操作系统 API 配合使用。

异常

1,2)
noexcept 规范:
noexcept

注释

转换函数 (3) 的提供使得接受 std::basic_string 文件名参数的标准文件打开 API(例如 std::ifstream 的构造函数)能够直接使用路径名而无需修改代码:

fs::path p = "/tmp/text.txt";
std::ifstream f(p);

示例

#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

参见

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