<experimental/filesystem> 路徑類別

Important

本頁記錄標頭所提供的<experimental/filesystem>已移除的 Microsoft 預標準std::experimental::filesystem::path實作。 它 不是 C++17 std::filesystem::path 類別。 此預標準實作基於 檔案系統技術規範 (N4100),早於 C++17 std::filesystem版本。 <experimental/filesystem>標頭與std::experimental::filesystem命名空間自 Microsoft Visual C++ 工具組 14.51 起被移除。 關於目前支援的 C++17 實作,請參見 path 類別。

該 path 類別儲存一個型別 string_type為 的物件,此處為說明目的所用 myname ,適合作為路徑名稱使用。 string_type是 的basic_string<value_type>同義詞,其中 value_type 是 在 Windows 上的wchar_t同義詞。

語法

class path;

建構函數

建構函式 Description
path 建構一個 path。

Typedefs(類型定義)

類型名稱 Description
const_iterator iterator 的同義字。
iterator 一個雙向常數迭代器,用以指定 path 的 myname組成部分。
string_type 此類型是 basic_string<value_type>的同義字。

成員函式

成員函數 Description
append 將指定的序列附加於 myname轉換後,並視需要插入preferred_separator。
assign 替換 myname 為指定的序列,並視需要轉換。
begin 回傳 a path::iterator 以指定路徑名稱中的第一個路徑元素(若存在)。
c_str 回傳指向 中的 myname第一個字元的指標。
clear 執行 myname.clear()。
compare 回傳比較值。
concat 根據需要將指定的序列附加於 myname已轉換(但不插入分隔符)。
empty 傳回 myname.empty()。
end 回傳一個型別 iterator為 的序列末迭代器。
extension 回傳 的後綴 filename()。
filename 回傳 的 myname檔名組件,具體來說 empty() ? path() : *--end()是 。 該元件可能是空的。
generic_string 回放this->string<Elem, Traits, Alloc>(al)時(在 Windows 下)任何轉為前斜線的反斜線。
generic_u16string 回放u16string()時(在 Windows 下)任何轉為前斜線的反斜線。
generic_u32string 在歷史的 MSVC 實作中已刪除。
generic_u8string 回放u8string()時(在 Windows 下)任何轉為前斜線的反斜線。
generic_wstring 回放wstring()時(在 Windows 下)任何轉為前斜線的反斜線。
has_extension 傳回 !extension().empty()。
has_filename 傳回 !filename().empty()。
has_parent_path 傳回 !parent_path().empty()。
has_relative_path 傳回 !relative_path().empty()。
has_root_directory 傳回 !root_directory().empty()。
has_root_name 傳回 !root_name().empty()。
has_root_path 傳回 !root_path().empty()。
has_stem 傳回 !stem().empty()。
is_absolute 對於 Windows,函式會回傳 has_root_name() && has_root_directory()。 對於 POSIX,函數回傳 has_root_directory()。
is_relative 傳回 !is_absolute()。
make_preferred 根據需要將每個分隔符轉換成 a preferred_separator 。
native 回傳路徑的原生表示。
parent_path 回傳 的 myname父路徑元件。
preferred_separator 常數物件會根據宿主作業系統提供分離路徑元件的首選字元。
relative_path 回傳 的 myname相對路徑分量。
remove_filename 移除檔名。
replace_extension 取代 的 myname延伸。
replace_filename 替換檔名。
root_directory 回傳 的 myname根目錄元件。
root_name 回傳 的 myname根名稱組件。
root_path 回傳 的 myname根路徑分量。
stem 回傳 stem 的 myname分量。
string 將儲存在 myname中的序列轉換。
swap 執行 swap(myname, right.myname)。
u16string 將儲存在 的 myname 序列轉換為 UTF-16,並回傳儲存在 型別 u16string為 的物件中。
u32string 在歷史的 MSVC 實作中已刪除。
u8string 將儲存在 的 myname 序列轉換為 UTF-8,並回傳儲存在 型別 u8string為 的物件中。
value_type 該類型描述主機作業系統偏好的路徑元素。
wstring 將儲存在 中的 myname 序列轉換為宿主系統 wchar_t 偏好的序列編碼,並將其回傳為 的 wstring物件類型。

Operators

Operator Description
operator= 將路徑的元素替換為另一條路徑的複製品。
operator+= 各種 concat 表情。
operator/= 各種 append 表情。
operator string_type 傳回 myname。

Requirements

標頭:<experimental/filesystem>

命名空間:std::experimental::filesystem

path::append

將指定的序列附加於 myname轉換後,並根據需要插入 a preferred_separator 。

template <class Source>
path& append(const Source& source);

template <class InIt>
path& append(InIt first, InIt last);

參數

source
指定順序。

first
開始指定序列。

last
指定序列結束。

path::assign

替換 myname 為指定的序列,並視需要轉換。

template <class Source>
path& assign(const Source& source);

template <class InIt>
path& assign(InIt first, InIt last);

參數

source
指定順序。

first
開始指定序列。

last
指定序列結束。

path::begin

回傳 a path::iterator 以指定路徑名稱中的第一個路徑元素(若存在)。

iterator begin() const;

path::c_str

回傳指向 中的 myname第一個字元的指標。

const value_type* c_str() const noexcept;

path::clear

執行 myname.clear()。

void clear() noexcept;

path::compare

第一個函數返回 myname.compare(pval.native())。 第二個函數返回 myname.compare(str)。 第三個函數返回 myname.compare(ptr)。

int compare(const path& pval) const noexcept;
int compare(const string_type& str) const;
int compare(const value_type *ptr) const;

參數

pval
比較的路徑。

str
用繩子比較。

ptr
給個比較指標。

path::concat

根據需要將指定的序列附加於 myname已轉換(但不插入分隔符)。

template <class Source>
path& concat(const Source& source);

template <class InIt>
path& concat(InIt first, InIt last);

參數

source
指定順序。

first
開始指定序列。

last
指定序列結束。

path::const_iterator

iterator 的同義字。

typedef iterator const_iterator;

path::empty

傳回 myname.empty()。

bool empty() const noexcept;

path::end

回傳一個型別 iterator為 的序列末迭代器。

iterator end() const;

path::extension

回傳 的後綴 filename()。

path extension() const;

註解

回傳 的 filename() X 後綴,使得:

若 X == path(".") || X == path("..") 或若 X 不包含點,則後綴為空。

否則,後綴以最右邊的點開頭(並包含)。

path::filename

回傳 的 myname檔名組件,具體來說 empty() ? path() : *--end()是 。 該元件可能是空的。

path filename() const;

path::generic_string

回放this->string<Elem, Traits, Alloc>(al)時(在 Windows 下)任何轉為前斜線的反斜線。

template <class Elem,
    class Traits = char_traits<Elem>,
    class Alloc = allocator<Elem>>
  basic_string<Elem, Traits, Alloc>
    generic_string(const Alloc& al = Alloc()) const;

string generic_string() const;

path::generic_u16string

回放u16string()時(在 Windows 下)任何轉為前斜線的反斜線。

u16string generic_u16string() const;

path::generic_u32string

此函式在歷史的 MSVC 實作中被刪除。

u32string generic_u32string() const = delete;

path::generic_u8string

回放u8string()時(在 Windows 下)任何轉為前斜線的反斜線。

string generic_u8string() const;

path::generic_wstring

回放wstring()時(在 Windows 下)任何轉為前斜線的反斜線。

wstring generic_wstring() const;

path::has_extension

傳回 !extension().empty()。

bool has_extension() const;

path::has_filename

傳回 !filename().empty()。

bool has_filename() const;

path::has_parent_path

傳回 !parent_path().empty()。

bool has_parent_path() const;

path::has_relative_path

傳回 !relative_path().empty()。

bool has_relative_path() const;

path::has_root_directory

傳回 !root_directory().empty()。

bool has_root_directory() const;

path::has_root_name

傳回 !root_name().empty()。

bool has_root_name() const;

path::has_root_path

傳回 !root_path().empty()。

bool has_root_path() const;

path::has_stem

傳回 !stem().empty()。

bool has_stem() const;

path::is_absolute

對於 Windows,函式會回傳 has_root_name() && has_root_directory()。 對於 POSIX,函數回傳 has_root_directory()。

bool is_absolute() const;

path::is_relative

傳回 !is_absolute()。

bool is_relative() const;

path::iterator

一個雙向常數迭代器,用以指定 的 myname路徑分量。

class iterator
   {
   // bidirectional iterator for path
   typedef bidirectional_iterator_tag iterator_category;
   typedef path_type value_type;
   typedef ptrdiff_t difference_type;
   typedef const value_type *pointer;
   typedef const value_type& reference;
   // ...
   };

註解

該類別描述了一個雙向常數迭代器,用以指定 path 序列中的 分 myname 量:

  1. 如果存在,則為根名

  2. 根目錄(若存在)

  3. 父目錄的其餘元素 path(若存在)則以檔名結尾(若存在)

對於 pval 類型的 path物件:

  1. path::iterator X = pval.begin() 若存在,則指定路徑名稱中的第一個 path 元素。

  2. X == pval.end() 是 true 當 X 分量序列結束後剛好點的時。

  3. *X 回傳一個與當前元件相符的字串

  4. ++X 指定序列中的下一個組件(若存在)。

  5. --X 指定序列中前一個組件(若存在)。

  6. 改變 myname 會使所有指定元素的 myname迭代器失效。

path::make_preferred

根據需要將每個分隔符轉換成 a preferred_separator 。

path& make_preferred();

path::native

取得路徑的原生字串表示。

const string_type& native() const noexcept;

註解

路徑可提供可攜式通用格式(參見 generic_string()),或路徑的原生格式。 此函式回傳原生字串。 在 POSIX 系統中,通用格式與原生格式相同。

在以下在 Windows 上執行的範例中,通用路徑字串為 c:/t/temp.txt ,原生字串為 c:\t\temp.txt。

// Compile with /std:c++14 and _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING defined.
#include <experimental/filesystem>

int main()
{
    std::experimental::filesystem::path p(LR"(c:\t\temp.txt)");
    auto native = p.native();          // Windows: L"c:\\t\\temp.txt"
    auto generic = p.generic_string(); // Windows: "c:/t/temp.txt"
}

path::operator=

將路徑的元素替換為另一條路徑的複製品。

path& operator=(const path& right);
path& operator=(path&& right) noexcept;

template <class Source>
path& operator=(const Source& source);

參數

right
這個path存在被複製進了path

source
來源 path。

註解

第一個成員運算子複製 right.myname 為 myname。 第二個成員運算子移動 right.myname 到 myname。 第三個成員運算子的行為與 相同 *this = path(source)。

path::operator+=

各種 concat 表情。

path& operator+=(const path& right);
path& operator+=(const string_type& str);
path& operator+=(const value_type *ptr);
path& operator+=(value_type elem);

template <class Source>
path& operator+=(const Source& source);

template <class Elem>
path& operator+=(Elem elem);

參數

right
額外的路徑。

str
多了一條線。

ptr
額外的提示。

elem
加法 value_type 或 Elem。

source
額外的來源。

註解

成員函數的行為與以下對應的表達式相同:

  1. concat(right);

  2. concat(path(str));

  3. concat(ptr);

  4. concat(string_type(1, elem));

  5. concat(source);

  6. concat(path(basic_string<Elem>(1, elem)));

path::operator/=

各種 append 表情。

path& operator/=(const path& right);

template <class Source>
path& operator/=(const Source& source);

參數

right
額外的路徑。

source
額外的來源。

註解

成員函數的行為與以下對應的表達式相同:

  1. append(right);

  2. append(source);

path::operator string_type

傳回 myname。

operator string_type() const;

path::parent_path

回傳 的 myname父路徑元件。

path parent_path() const;

註解

移除目錄分隔符及緊鄰目錄分隔符後filename().native(),會回傳父myname路徑元件,具體來說是前myname綴的前綴。 (同樣地,若 begin() != end(),則是透過依序應用operator/=將該區間[begin(), --end())所有元素結合。)該元件可能是空的。

path::path

以各種方式構造 a path 。

path();

path(const path& right);
path(path&& right) noexcept;

template <class Source>
path(const Source& source);

template <class Source>
path(const Source& source, const locale& loc);

template <class InIt>
path(InIt first, InIt last);

template <class InIt>
path(InIt first, InIt last, const locale& loc);

參數

right
所建構路徑的路徑是複製品。

source
所建構路徑的來源應該是複製品。

loc
指定地點。

first
要複製之第一個元素的位置。

last
最後一個要複製元素的位置。

註解

構造子以不同方式構造 myname :

因為它 path() 是 myname()。

對於 path(const path& right,它就是 myname(right.myname)。

因為它 path(path&& right) 是 myname(right.myname)。

因為它 template<class Source> path(const Source& source) 是 myname(source)。

因為 template<class Source> path(const Source& source, const locale& loc)myname(source),從 中獲得所需的 codecvt 任何面 loc。

因為它 template<class InIt> path(InIt first, InIt last) 是 myname(first, last)。

因為 template<class InIt> path(InIt first, InIt last, const locale& loc)myname(first, last),從 中獲得所需的 codecvt 任何面 loc。

path::preferred_separator

常數物件會根據宿主作業系統提供分離路徑元件的首選字元。

#if _WIN32_C_LIB
static constexpr value_type preferred_separator = L'\\';
#else // assume POSIX
static constexpr value_type preferred_separator = '/';
#endif // filesystem model now defined

註解

在大多數 Windows 環境下,它L'/'同樣被允許取代。

path::relative_path

回傳 的 myname相對路徑分量。

path relative_path() const;

註解

移除後回傳相對myname路徑成分,特別是移除mynameroot_path().native()後續冗餘目錄分隔符後的後綴。 該元件可能是空的。

path::remove_filename

移除檔名。

path& remove_filename();

path::replace_extension

取代 的 myname延伸。

path& replace_extension(const path& newext = path());

參數

newext
新的分機。

註解

首先移除 中的myname後綴extension().native()。 若(!newext.empty() && newext[0] != dot其中點為*path(".").c_str()),則點會附加於myname。 然後 newext 附加於 myname。

path::replace_filename

替換檔名。

path& replace_filename(const path& pval);

參數

pval
檔名的路徑。

註解

成員函式執行:

remove_filename();

*this /= pval;
return (*this);

path::root_directory

回傳 的 myname根目錄元件。

path root_directory() const;

註解

該元件可能是空的。

path::root_name

回傳 的 myname根名稱組件。

path root_name() const;

註解

該元件可能是空的。

path::root_path

回傳 的 myname根路徑分量。

path root_path() const;

註解

回傳 的 myname根路徑分量,具體來說 root_name() / root_directory是 。 該元件可能是空的。

path::stem

回傳 stem 的 myname分量。

path stem() const;

註解

返回 stem 的 myname分量,特別是 filename().native() 移除任何尾隨的 extension().native() 部分。 該元件可能是空的。

path::string

將儲存在 myname中的序列轉換。

template <class Elem, class Traits = char_traits<Elem>, class Alloc = allocator<Elem>>
basic_string<Elem, Traits, Alloc> string(const Alloc& al = Alloc()) const;
string string() const;

註解

第一個(模板)成員函數以相同方式轉換儲存 myname 的序列:

  1. string() 的 string<char, Traits, Alloc>()

  2. wstring() 的 string<wchar_t, Traits, Alloc>()

  3. u16string() 的 string<char16_t, Traits, Alloc>()

第二個成員函數將儲存在 中的 myname 序列轉換為宿主系統 char 偏好的序列編碼,並將其回傳為 的物件 string類型。

path::string_type

此類型是 basic_string<value_type>的同義字。

typedef basic_string<value_type> string_type;

path::swap

執行 swap(myname, right.myname)。

void swap(path& right) noexcept;

path::u16string

將儲存在 的 myname 序列轉換為 UTF-16,並回傳儲存在 型別 u16string為 的物件中。

u16string u16string() const;

path::u32string

此函式在歷史的 MSVC 實作中被刪除。

u32string u32string() const = delete;

path::u8string

將儲存在 的 myname 序列轉換為 UTF-8,並回傳儲存在 型別 u8string為 的物件中。

string u8string() const;

path::value_type

該類型描述 path 主機作業系統偏好的元素。

#if _WIN32_C_LIB
typedef wchar_t value_type;
#else // assume POSIX
typedef char value_type;
#endif // filesystem model now defined

path::wstring

將儲存在 中的 myname 序列轉換為宿主系統 wchar_t 偏好的序列編碼,並將其回傳為 的 wstring物件類型。

wstring wstring() const;

參見

<experimental/filesystem>
<experimental/filesystem> 操作員