名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | experimental‎ | fs‎ | path
 
 
實驗性
技術規範
檔案系統庫 (檔案系統 TS)
庫基礎 (庫基礎 TS)
庫基礎 2 (庫基礎 TS v2)
庫基礎 3 (庫基礎 TS v3)
並行性擴充套件 (並行性 TS)
並行性擴充套件 2 (並行性 TS v2)
併發性擴充套件 (併發性 TS)
併發擴充套件 2 (concurrency TS v2)
概念 (概念 TS)
範圍 (範圍 TS)
反射 (反射 TS)
數學特殊函式 (特殊函式 TR)
實驗性非 TS
模式匹配
線性代數
std::execution
契約
2D 圖形
 
 
 
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");
 
    // 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

[編輯] 參閱

返回轉換為字串的原生路徑名格式的路徑
(公共成員函式) [編輯]
返回轉換為字串的通用路徑名格式的路徑
(公共成員函式) [編輯]