description | title | ms.date | f1_keywords | |
---|---|---|---|---|
Learn more about: path Class | path Class | 06/17/2022 |
|
The path
class stores an object of type string_type
, called myname
here for the purposes of exposition, suitable for use as a pathname. string_type
is a synonym for basic_string<value_type>
, where value_type
is a synonym for wchar_t
on Windows or char
on POSIX.
For more information, and code examples, see File System Navigation (C++).
classpath;
Constructor | Description |
---|---|
path | Constructs a path . |
Type name | Description |
---|---|
const_iterator | A synonym for iterator . |
iterator | A bidirectional constant iterator that designates the path components of myname . |
string_type | The type is a synonym for basic_string<value_type> . |
Member function | Description |
---|---|
append | Appends the specified sequence to mypath , converted and inserting a preferred_separator as needed. |
assign | Replaces mypath with the specified sequence, converted as needed. |
begin | Returns a path::iterator designating the first path element in the pathname, if present. |
c_str | Returns a pointer to the first character in mypath . |
clear | Executes mypath.clear() . |
compare | Returns comparison values. |
concat | Appends the specified sequence to mypath , converted (but not inserting a separator) as needed. |
empty | Returns mypath.empty() . |
end | Returns an end-of-sequence iterator of type iterator . |
extension | Returns the suffix of filename() . |
filename | Returns the root directory component of myname , specifically empty() ? path() : *--end() . The component may be empty. |
generic_string | Returns this->string<Elem, Traits, Alloc>(al) with (under Windows) any backslash converted to a forward slash. |
generic_u16string | Returns u16string() with (under Windows) any backslash converted to a forward slash. |
generic_u32string | Returns u32string() with (under Windows) any backslash converted to a forward slash. |
generic_u8string | Returns u8string() with (under Windows) any backslash converted to a forward slash. |
generic_wstring | Returns wstring() with (under Windows) any backslash converted to a forward slash. |
has_extension | Returns !extension().empty() . |
has_filename | Returns !filename().empty() . |
has_parent_path | Returns !parent_path().empty() . |
has_relative_path | Returns !relative_path().empty() . |
has_root_directory | Returns !root_directory().empty() . |
has_root_name | Returns !root_name().empty() . |
has_root_path | Returns !root_path().empty() . |
has_stem | Returns !stem().empty() . |
is_absolute | For Windows, the function returns has_root_name() && has_root_directory() . For POSIX, the function returns has_root_directory() . |
is_relative | Returns !is_absolute() . |
make_preferred | Converts each separator to a preferred_separator as needed. |
native | Returns the native representation of the path. |
parent_path | Returns the parent path component of myname . |
preferred_separator | The constant object gives the preferred character for separating path components, depending on the host operating system. |
relative_path | Returns the relative path component of myname . |
remove_filename | Removes the filename. |
replace_extension | Replaces the extension of myname . |
replace_filename | Replaces the filename. |
root_directory | Returns the root directory component of myname . |
root_name | Returns the root name component of myname . |
root_path | Returns the root path component of myname . |
stem | Returns the stem component of myname . |
string | Converts the sequence stored in mypath . |
swap | Executes swap(mypath, right.mypath) . |
u16string | Converts the sequence stored in mypath to UTF-16 and returns it stored in an object of type u16string . |
u32string | Converts the sequence stored in mypath to UTF-32 and returns it stored in an object of type u32string . |
u8string | Converts the sequence stored in mypath to UTF-8 and returns it stored in an object of type u8string . |
value_type | The type describes the path elements favored by the host operating system. |
wstring | Converts the sequence stored in mypath to the encoding favored by the host system for a wchar_t sequence and returns it stored in an object of type wstring . |
Operator | Description |
---|---|
operator= | Replaces the elements of the path with a copy of another path. |
operator+= | Various concat expressions. |
operator/= | Various append expressions. |
operator string_type | Returns myname . |
Header:<filesystem>
Namespace:std::experimental::filesystem
Appends the specified sequence to mypath
, converted and inserting a preferred_separator
as needed.
template <classSource> path& append(const Source& source); template <classInIt> path& append(InIt first, InIt last);
source
Specified sequence.
first
Start of specified sequence.
last
End of specified sequence.
Replaces mypath
with the specified sequence, converted as needed.
template <classSource> path& assign(const Source& source); template <classInIt> path& assign(InIt first, InIt last);
source
Specified sequence.
first
Start of specified sequence.
last
End of specified sequence.
Returns a path::iterator
designating the first path element in the pathname, if present.
iterator begin() const;
Returns a pointer to the first character in mypath
.
const value_type& *c_str() constnoexcept;
Executes mypath.clear()
.
voidclear() noexcept;
The first function returns mypath.compare(pval.native())
. The second function returns mypath.compare(str)
. The third function returns mypath.compare(ptr)
.
intcompare(const path& pval) constnoexcept; intcompare(const string_type& str) const; intcompare(const value_type *ptr) const;
pval
Path to compare.
str
String to compare.
ptr
Pointer to compare.
Appends the specified sequence to mypath
, converted (but not inserting a separator) as needed.
template <classSource> path& concat(const Source& source); template <classInIt> path& concat(InIt first, InIt last);
source
Specified sequence.
first
Start of specified sequence.
last
End of specified sequence.
A synonym for iterator
.
typedef iterator const_iterator;
Returns mypath.empty()
.
boolempty() constnoexcept;
Returns an end-of-sequence iterator of type iterator
.
iterator end() const;
Returns the suffix of filename()
.
path extension() const;
Returns the suffix of filename() X
such that:
If X == path(".") || X == path("..")
or if X
contains no dot, the suffix is empty.
Otherwise, the suffix begins with (and includes) the rightmost dot.
Returns the root directory component of myname
, specifically empty() path() : *--end()
. The component may be empty.
path filename() const;
Returns this->string<Elem, Traits, Alloc>(al)
with (under Windows) any backslash converted to a forward slash.
template <classElem, classTraits = char_traits<Elem>, classAlloc = allocator<Elem>> basic_string<Elem, Traits, Alloc> generic_string(const Alloc& al = Alloc()) const; string generic_string() const;
Returns u16string()
with (under Windows) any backslash converted to a forward slash.
u16string generic_u16string() const;
Returns u32string()
with (under Windows) any backslash converted to a forward slash.
u32string generic_u32string() const;
Returns u8string()
with (under Windows) any backslash converted to a forward slash.
string generic_u8string() const;
Returns wstring()
with (under Windows) any backslash converted to a forward slash.
wstring generic_wstring() const;
Returns !extension().empty()
.
boolhas_extension() const;
Returns !filename().empty()
.
boolhas_filename() const;
Returns !parent_path().empty()
.
boolhas_parent_path() const;
Returns !relative_path().empty()
.
boolhas_relative_path() const;
Returns !root_directory().empty()
.
boolhas_root_directory() const;
Returns !root_name().empty()
.
boolhas_root_name() const;
Returns !root_path().empty()
.
boolhas_root_path() const;
Returns !stem().empty()
.
boolhas_stem() const;
For Windows, the function returns has_root_name() && has_root_directory()
. For POSIX, the function returns has_root_directory()
.
boolis_absolute() const;
Returns !is_absolute()
.
boolis_relative() const;
A bidirectional constant iterator that designates the path components of myname
.
classiterator { // bidirectional iterator for pathtypedef bidirectional_iterator_tag iterator_category; typedef path_type value_type; typedefptrdiff_t difference_type; typedefconst value_type *pointer; typedefconst value_type& reference; // ... };
The class describes a bidirectional constant iterator that designates the path
components of myname
in the sequence:
the root name, if present
the root directory, if present
the remaining directory elements of the parent
path
, if present, ending with the filename, if present
For pval
an object of type path
:
path::iterator X = pval.begin()
designates the firstpath
element in the pathname, if present.X == pval.end()
istrue
whenX
points just past the end of the sequence of components.*X
returns a string that matches the current component++X
designates the next component in the sequence, if present.--X
designates the preceding component in the sequence, if present.Altering
myname
invalidates all iterators designating elements inmyname
.
Converts each separator to a preferred_separator
as needed.
path& make_preferred();
Get the native string representation of the path.
const string_type& native() constnoexcept;
The path is available in a portable generic format (see generic_string()
) or the native format of the path. This function returns the native string. On a POSIX system, the generic format and the native format are the same.
In the following example running on Windows 11, the generic path string is c:/t/temp/temp.txt
and the native string is c:\\t\\temp.txt
// Compile with /std:c++17 or higher #include<filesystem>intmain() { std::filesystem::path p(R"(c:\t\temp.txt)"); auto native = p.native(); // Windows: L"c:\\t\temp.txt"auto generic = p.generic_string(); // Windows: "c:/t/temp.txt" }
Replaces the elements of the path with a copy of another path.
path& operator=(const path& right); path& operator=(path&& right) noexcept; template <classSource> path& operator=(const Source& source);
right
The path
being copied into the path
.
source
The source path
.
The first member operator copies right.myname
to myname
. The second member operator moves right.myname
to myname
. The third member operator behaves the same as *this = path(source)
.
Various concat
expressions.
path& operator+=(const path& right); path& operator+=(const string_type& str); path& operator+=(const value_type *ptr); path& operator+=(value_type elem); template <classSource> path& operator+=(const Source& source); template <classElem> path& operator+=(Elem elem);
right
The added path.
str
The added string.
ptr
The added pointer.
elem
The added value_type
or Elem
.
source
The added source.
The member functions behave the same as the following corresponding expressions:
concat(right);
concat(path(str));
concat(ptr);
concat(string_type(1, elem));
concat(source);
concat(path(basic_string<Elem>(1, elem)));
Various append
expressions.
path& operator/=(const path& right); template <classSource> path& operator/=(const Source& source);
right
The added path.
source
The added source.
The member functions behave the same as the following corresponding expressions:
append(right);
append(source);
Returns myname
.
operatorstring_type() const;
Returns the parent path component of myname
.
path parent_path() const;
Returns the parent path component of myname
, specifically the prefix of myname
after removing filename().native()
and any immediately preceding directory separators. (Equally, if begin() != end()
, it's the combining of all elements in the range [begin(), --end())
by successively applying operator/=
.) The component may be empty.
Constructs a path
in various ways.
path(); path(const path& right); path(path&& right) noexcept; template <classSource> path(const Source& source); template <classSource> path(const Source& source, const locale& loc); template <classInIt> path(InIt first, InIt last); template <classInIt> path(InIt first, InIt last, const locale& loc);
right
The path of which the constructed path is to be a copy.
source
The source of which the constructed path is to be a copy.
loc
The specified locale.
first
The position of the first element to be copied.
last
The position of the last element to be copied.
The constructors all construct myname
in various ways:
For path()
it's myname()
.
For path(const path& right
) it's myname(right.myname)
.
For path(path&& right)
it's myname(right.myname)
.
For template<class Source> path(const Source& source)
it's myname(source)
.
For template<class Source> path(const Source& source, const locale& loc)
it's myname(source)
, obtaining any needed codecvt
facets from loc
.
For template<class InIt> path(InIt first, InIt last)
it's myname(first, last)
.
For template<class InIt> path(InIt first, InIt last, const locale& loc)
it's myname(first, last)
, obtaining any needed codecvt
facets from loc
.
The constant object gives the preferred character for separating path components, depending on the host operating system.
#if _WIN32_C_LIB staticconstexpr value_type preferred_separator == L'\\'; #else// assume POSIXstaticconstexpr value_type preferred_separator == '/'; #endif// filesystem model now defined
It's equally permissible in most contexts under Windows to use L'/'
in its place.
Returns the relative path component of myname
.
path relative_path() const;
Returns the relative path component of myname
, specifically the suffix of myname
after removing root_path().native()
and any immediately subsequent redundant directory separators. The component may be empty.
Removes the filename.
path& remove_filename();
Replaces the extension of myname
.
path& replace_extension(const path& newext = path());
newext
The new extension.
First removes the suffix extension().native()
from myname
. Then if !newext.empty() && newext[0] != dot
(where dot is *path(".").c_str()
), then dot is appended to myname
. Then newext
is appended to myname
.
Replaces the filename.
path& replace_filename(const path& pval);
pval
The path of the filename.
The member function executes:
remove_filename(); *this /= pval; return (*this);
Returns the root directory component of myname
.
path root_directory() const;
The component may be empty.
Returns the root name component of myname
.
path root_name() const;
The component may be empty.
Returns the root path component of myname
.
path root_path() const;
Returns the root path component of myname
, specifically root_name()
/ root_directory
. The component may be empty.
Returns the stem
component of myname
.
path stem() const;
Returns the stem
component of myname
, specifically filename().native()
with any trailing extension().native()
removed. The component may be empty.
Converts the sequence stored in mypath
.
template \<classElem, classTraits = char_traits\<Elem>, classAlloc = allocator\<Elem>> basic_string\<Elem, Traits, Alloc> string(const Alloc& al = Alloc()) const; string string() const;
The first (template) member function converts the sequence stored in mypath
the same way as:
string()
forstring<char, Traits, Alloc>()
wstring()
forstring<wchar_t, Traits, Alloc>()
u16string()
forstring<char16_t, Traits, Alloc>()
u32string()
forstring<char32_t, Traits, Alloc>()
The second member function converts the sequence stored in mypath
to the encoding favored by the host system for a char
sequence and returns it stored in an object of type string
.
The type is a synonym for basic_string<value_type>
.
typedef basic_string<value_type> string_type;
Executes swap(mypath, right.mypath)
.
voidswap(path& right) noexcept;
Converts the sequence stored in mypath
to UTF-16 and returns it stored in an object of type u16string
.
u16string u16string() const;
Converts the sequence stored in mypath
to UTF-32 and returns it stored in an object of type u32string
.
u32string u32string() const;
Converts the sequence stored in mypath
to UTF-8 and returns it stored in an object of type u8string
.
string u8string() const;
The type describes the path
elements favored by the host operating system.
#if _WIN32_C_LIB typedefwchar_t value_type; #else// assume POSIXtypedefchar value_type; #endif// filesystem model now defined
Converts the sequence stored in mypath
to the encoding favored by the host system for a wchar_t
sequence and returns it stored in an object of type wstring
.
wstring wstring() const;