Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
The path class described here is the C++17 std::filesystem::path class in the <filesystem> header.
For more information, and code examples, see File System Navigation (C++).
Syntax
class path;
Constructors
| Constructor | Description |
|---|---|
path |
Constructs a path. |
Typedefs
| 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>. |
Enumerations
| Type name | Description |
|---|---|
format |
Specifies the pathname format for constructors: auto_format, native_format, or generic_format. |
Member functions
| Member function | Description |
|---|---|
append |
Appends the specified sequence to myname, converted and inserting a preferred_separator as needed. |
assign |
Replaces myname 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 myname. |
clear |
Executes myname.clear(). |
compare |
Returns comparison values. |
concat |
Appends the specified sequence to myname, converted (but not inserting a separator) as needed. |
empty |
Returns myname.empty(). |
end |
Returns an end-of-sequence iterator of type iterator. |
extension |
Returns the suffix of filename(). |
filename |
Returns the filename 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(). |
lexically_normal |
Returns myname converted to normal form. |
lexically_proximate |
Returns lexically_relative(base), or *this if that result is empty. |
lexically_relative |
Returns myname made relative to a base path, using lexical (string) analysis only. |
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 myname. |
swap |
Executes swap(myname, right.myname). |
u16string |
Converts the sequence stored in myname to UTF-16 and returns it stored in an object of type u16string. |
u32string |
Converts the sequence stored in myname to UTF-32 and returns it stored in an object of type u32string. |
u8string |
Converts the sequence stored in myname 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 myname to the encoding favored by the host system for a wchar_t sequence and returns it stored in an object of type wstring. |
Operators
| 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. |
Requirements
Header: <filesystem>
Namespace: std::filesystem
path::append
Appends the specified sequence to myname, converted and inserting a preferred_separator as needed.
template <class Source>
path& append(const Source& source);
template <class InIt>
path& append(InIt first, InIt last);
Parameters
source
Specified sequence.
first
Start of specified sequence.
last
End of specified sequence.
path::assign
Replaces myname with the specified sequence, converted as needed.
path& assign(string_type&& source);
template <class Source>
path& assign(const Source& source);
template <class InIt>
path& assign(InIt first, InIt last);
Parameters
source
Specified sequence.
first
Start of specified sequence.
last
End of specified sequence.
path::begin
Returns a path::iterator designating the first path element in the pathname, if present.
iterator begin() const;
path::c_str
Returns a pointer to the first character in myname.
const value_type* c_str() const noexcept;
path::clear
Executes myname.clear().
void clear() noexcept;
path::compare
The first function compares the elements of this path with pval. The other functions construct a path from their argument and return the result of comparing it with this path.
int compare(const path& pval) const noexcept;
int compare(const string_type& str) const;
int compare(basic_string_view<value_type> str) const;
int compare(const value_type *ptr) const;
Parameters
pval
Path to compare.
str
String to compare.
ptr
Pointer to compare.
path::concat
Appends the specified sequence to myname, converted (but not inserting a separator) as needed.
template <class Source>
path& concat(const Source& source);
template <class InIt>
path& concat(InIt first, InIt last);
Parameters
source
Specified sequence.
first
Start of specified sequence.
last
End of specified sequence.
path::const_iterator
A synonym for iterator.
typedef iterator const_iterator;
path::empty
Returns myname.empty().
bool empty() const noexcept;
path::end
Returns an end-of-sequence iterator of type iterator.
iterator end() const;
path::extension
Returns the suffix of filename().
path extension() const;
Remarks
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.
path::filename
Returns the filename component of myname, specifically empty() ? path() : *--end(). The component may be empty.
path filename() const;
path::format
Specifies the pathname format used to interpret the character sequence passed to a path constructor.
enum format {
auto_format,
native_format,
generic_format
};
Members
| Enumerator | Description |
|---|---|
auto_format |
The implementation autodetects the format. This is the default for the constructors that take a format argument. |
native_format |
Interpret the sequence in the native pathname format. |
generic_format |
Interpret the sequence in the generic pathname format. |
Remarks
Because the native format and the generic format are interchangeable on Windows, the Microsoft implementation treats all three enumerators the same. The format parameter is provided for standard conformance and portability. For more information, see the WG21 N4659 [fs.class.path] specification.
path::generic_string
Returns this->string<Elem, Traits, Alloc>(al) with (under Windows) any backslash converted to a forward slash.
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
Returns u16string() with (under Windows) any backslash converted to a forward slash.
u16string generic_u16string() const;
path::generic_u32string
Returns u32string() with (under Windows) any backslash converted to a forward slash.
u32string generic_u32string() const;
path::generic_u8string
Returns u8string() with (under Windows) any backslash converted to a forward slash.
string generic_u8string() const;
path::generic_wstring
Returns wstring() with (under Windows) any backslash converted to a forward slash.
wstring generic_wstring() const;
path::has_extension
Returns !extension().empty().
bool has_extension() const;
path::has_filename
Returns !filename().empty().
bool has_filename() const;
path::has_parent_path
Returns !parent_path().empty().
bool has_parent_path() const;
path::has_relative_path
Returns !relative_path().empty().
bool has_relative_path() const;
path::has_root_directory
Returns !root_directory().empty().
bool has_root_directory() const;
path::has_root_name
Returns !root_name().empty().
bool has_root_name() const;
path::has_root_path
Returns !root_path().empty().
bool has_root_path() const;
path::has_stem
Returns !stem().empty().
bool has_stem() const;
path::is_absolute
For Windows, the function returns has_root_name() && has_root_directory(). For POSIX, the function returns has_root_directory().
bool is_absolute() const;
path::is_relative
Returns !is_absolute().
bool is_relative() const;
path::iterator
A bidirectional constant iterator that designates the path components of 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;
// ...
};
Remarks
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 firstpathelement in the pathname, if present.X == pval.end()istruewhenXpoints just past the end of the sequence of components.*Xreturns a string that matches the current component++Xdesignates the next component in the sequence, if present.--Xdesignates the preceding component in the sequence, if present.Altering
mynameinvalidates all iterators designating elements inmyname.
path::lexically_normal
Returns myname converted to normal form. The conversion is purely lexical: the filesystem isn't accessed.
path lexically_normal() const;
Remarks
Normalization collapses redundant elements such as . and .. and directory separators. For example, path("a/./b/../c").lexically_normal() yields a\c on Windows. An empty path normalizes to an empty path; a path that reduces to nothing normalizes to .. For the full normalization algorithm, see the WG21 N4659 [fs.path.generic] specification.
path::lexically_proximate
Returns the result of lexically_relative(base). If that result is an empty path, returns a copy of *this instead.
path lexically_proximate(const path& base) const;
Parameters
base
The base path to make *this proximate to.
path::lexically_relative
Returns myname made relative to base, using lexical (string) analysis only. The filesystem isn't accessed.
path lexically_relative(const path& base) const;
Parameters
base
The base path to make *this relative to.
Remarks
If root_name() != base.root_name(), or if one path is absolute and the other is relative, or if the path has a filename but no root directory while the base doesn't, returns an empty path. Otherwise, determines the relative path from base to *this by finding the first mismatched element and emitting .. for each remaining element of base, followed by the remaining elements of *this. For example, path("/a/d").lexically_relative("/a/b/c") yields ..\..\d. For the full algorithm, see the WG21 N4659 [fs.path.gen] specification.
path::make_preferred
Converts each separator to a preferred_separator as needed.
path& make_preferred();
path::native
Get the native string representation of the path.
const string_type& native() const noexcept;
Remarks
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, the generic path string is c:/t/temp.txt and the native string is c:\t\temp.txt.
// Compile with /std:c++17 or higher
#include <filesystem>
int main()
{
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"
}
path::operator=
Replaces the elements of the path with a copy of another path.
path& operator=(const path& right);
path& operator=(path&& right) noexcept;
path& operator=(string_type&& source);
template <class Source>
path& operator=(const Source& source);
Parameters
right
The path being copied into the path.
source
The source path.
Remarks
The first member operator copies right.myname to myname. The second member operator moves right.myname to myname. The third member operator moves source into myname. The fourth member operator behaves the same as *this = path(source).
path::operator+=
Various concat expressions.
path& operator+=(const path& right);
path& operator+=(const string_type& str);
path& operator+=(basic_string_view<value_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);
Parameters
right
The added path.
str
The added string.
ptr
The added pointer.
elem
The added value_type or Elem.
source
The added source.
Remarks
The member functions behave the same as calling concat with the argument. The path, string_type, basic_string_view, and null-terminated value_type* overloads concatenate the argument's character sequence. The value_type and Elem overloads concatenate a single character. The templated Source overload concatenates the converted source sequence.
path::operator/=
Various append expressions.
path& operator/=(const path& right);
template <class Source>
path& operator/=(const Source& source);
Parameters
right
The added path.
source
The added source.
Remarks
The member functions behave the same as the following corresponding expressions:
append(right);append(source);
path::operator string_type
Returns myname.
operator string_type() const;
path::parent_path
Returns the parent path component of myname.
path parent_path() const;
Remarks
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.
path::path
Constructs a path in various ways.
path() noexcept;
path(const path& right);
path(path&& right) noexcept;
path(string_type&& source, format fmt = auto_format);
template <class Source>
path(const Source& source, format fmt = auto_format);
template <class InIt>
path(InIt first, InIt last, format fmt = auto_format);
template <class Source>
path(const Source& source, const locale& loc, format fmt = auto_format);
template <class InIt>
path(InIt first, InIt last, const locale& loc, format fmt = auto_format);
Parameters
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.
fmt
The pathname format used to interpret source or the first/last range. Defaults to auto_format.
loc
The specified locale.
first
The position of the first element to be copied.
last
The position of the last element to be copied.
Remarks
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 path(string_type&& source, format fmt) it's myname(move(source)).
For template<class Source> path(const Source& source, format fmt) it's myname(source).
For template<class Source> path(const Source& source, const locale& loc, format fmt) it's myname(source), obtaining any needed codecvt facets from loc.
For template<class InIt> path(InIt first, InIt last, format fmt) it's myname(first, last).
For template<class InIt> path(InIt first, InIt last, const locale& loc, format fmt) it's myname(first, last), obtaining any needed codecvt facets from loc.
The fmt parameter selects the pathname format. On Windows, the native and generic formats are interchangeable, so the Microsoft implementation ignores fmt.
path::preferred_separator
The constant object gives the preferred character for separating path components, depending on the host operating system.
#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
Remarks
It's equally permissible in most contexts under Windows to use L'/' in its place.
path::relative_path
Returns the relative path component of myname.
path relative_path() const;
Remarks
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.
path::remove_filename
Removes the filename.
path& remove_filename();
path::replace_extension
Replaces the extension of myname.
path& replace_extension(const path& newext = path());
Parameters
newext
The new extension.
Remarks
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.
path::replace_filename
Replaces the filename.
path& replace_filename(const path& pval);
Parameters
pval
The path of the filename.
Remarks
The member function executes:
remove_filename();
*this /= pval;
return (*this);
path::root_directory
Returns the root directory component of myname.
path root_directory() const;
Remarks
The component may be empty.
path::root_name
Returns the root name component of myname.
path root_name() const;
Remarks
The component may be empty.
path::root_path
Returns the root path component of myname.
path root_path() const;
Remarks
Returns the root path component of myname, specifically root_name() / root_directory. The component may be empty.
path::stem
Returns the stem component of myname.
path stem() const;
Remarks
Returns the stem component of myname, specifically filename().native() with any trailing extension().native() removed. The component may be empty.
path::string
Converts the sequence stored in 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;
Remarks
The first (template) member function converts the sequence stored in myname 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 myname to the encoding favored by the host system for a char sequence and returns it stored in an object of type string.
path::string_type
The type is a synonym for basic_string<value_type>.
typedef basic_string<value_type> string_type;
path::swap
Executes swap(myname, right.myname).
void swap(path& right) noexcept;
path::u16string
Converts the sequence stored in myname to UTF-16 and returns it stored in an object of type u16string.
u16string u16string() const;
path::u32string
Converts the sequence stored in myname to UTF-32 and returns it stored in an object of type u32string.
u32string u32string() const;
path::u8string
Converts the sequence stored in myname to UTF-8 and returns it stored in an object of type u8string.
string u8string() const;
path::value_type
The type describes the path elements favored by the host operating system.
#if _WIN32_C_LIB
typedef wchar_t value_type;
#else // assume POSIX
typedef char value_type;
#endif // filesystem model now defined
path::wstring
Converts the sequence stored in myname 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;