![]() |
boost::filesystem::path — Filesystem path class.
// In header: <boost/filesystem/path.hpp> class path { public: // types typedef unspecified value_type; // Character type used by the operating system API to represent paths. typedef std::basic_string< value_type > string_type; // String type used by the operating system API to represent paths. typedef std::codecvt< wchar_t, char, std::mbstate_t > codecvt_type; // Character code conversion facet type used to convert path strings between character types. typedef unspecified iterator; // Iterator over path elements. typedef unspecified const_iterator; // Iterator over path elements. typedef unspecified reverse_iterator; // Reverse iterator over path elements. typedef unspecified const_reverse_iterator; // Reverse iterator over path elements. // constructors path() noexcept; path(path const &); path(path const &, codecvt_type const &); path(path &&) noexcept; path(path &&, codecvt_type const &) noexcept; template<typename Source> path(Source const &); template<typename Source> explicit path(Source const &, codecvt_type const &); path(string_type &&) noexcept; path(string_type &&, codecvt_type const &) noexcept; template<typename InputIterator> path(InputIterator, InputIterator); template<typename InputIterator> path(InputIterator, InputIterator, codecvt_type const &); path(std::nullptr_t) = delete; // assignment path & operator=(path const &); path & assign(path const &); path & assign(path const &, codecvt_type const &); path & operator=(path &&) noexcept; path & assign(path &&) noexcept; path & assign(path &&, codecvt_type const &) noexcept; path & operator=(string_type &&) noexcept; path & assign(string_type &&) noexcept; path & assign(string_type &&, codecvt_type const &) noexcept; template<typename Source> path & operator=(Source const &); template<typename Source> path & assign(Source const &); template<typename Source> path & assign(Source const &, codecvt_type const &); template<typename InputIterator> path & assign(InputIterator, InputIterator); template<typename InputIterator> path & assign(InputIterator, InputIterator, codecvt_type const &); path & operator=(std::nullptr_t) = delete; // concatenation template<typename Char> path & operator+=(Char); path & operator+=(path const &); path & concat(path const &); path & concat(path const &, codecvt_type const &); template<typename Source> path & operator+=(Source const &); template<typename Source> path & concat(Source const &); template<typename Source> path & concat(Source const &, codecvt_type const &); template<typename InputIterator> path & concat(InputIterator, InputIterator); template<typename InputIterator> path & concat(InputIterator, InputIterator, codecvt_type const &); // appending path & operator/=(path const &); path & append(path const &); path & append(path const &, codecvt_type const &); template<typename Source> path & operator/=(Source const &); template<typename Source> path & append(Source const &); template<typename Source> path & append(Source const &, codecvt_type const &); template<typename InputIterator> path & append(InputIterator, InputIterator); template<typename InputIterator> path & append(InputIterator, InputIterator, const codecvt_type &); // modifiers void clear() noexcept; path & make_preferred(); path & remove_filename(); path & remove_filename_and_trailing_separators(); path & remove_trailing_separator(); path & replace_filename(path const &); path & replace_extension(path const & = path()); void swap(path &) noexcept; // observers bool empty() const noexcept; bool filename_is_dot() const; bool filename_is_dot_dot() const; bool has_root_path() const; bool has_root_name() const; bool has_root_directory() const; bool has_relative_path() const; bool has_parent_path() const; bool has_filename() const; bool has_stem() const; bool has_extension() const; bool is_relative() const; bool is_absolute() const; string_type const & native() const noexcept; const value_type * c_str() const noexcept; string_type::size_type size() const noexcept; template<typename String> String string() const; template<typename String> String string(codecvt_type const &) const; std::string string() const; std::string string(codecvt_type const &) const; std::wstring wstring() const; std::wstring wstring(codecvt_type const &) const; path generic_path() const; template<typename String> String generic_string() const; template<typename String> String generic_string(codecvt_type const &) const; std::string generic_string() const; std::string generic_string(codecvt_type const &) const; std::wstring generic_wstring() const; std::wstring generic_wstring(codecvt_type const &) const; // comparison int compare(path const &) const; template<typename Source> int compare(Source const &) const; template<typename Source> int compare(Source const &, codecvt_type const &) const; // decomposition path root_path() const; path root_name() const; path root_directory() const; path relative_path() const; path parent_path() const; path filename() const; path stem() const; path extension() const; // lexical operations path lexically_normal() const; path lexically_relative(path const &) const; path lexically_proximate(path const &) const; // iterators iterator begin() const; iterator end() const; reverse_iterator rbegin() const; reverse_iterator rend() const; // locale operations static std::locale imbue(std::locale const &); static codecvt_type const & codecvt(); // public data members static value_type separator; static value_type preferred_separator; static value_type dot; };
An object of class path represents a path, and contains a pathname. Such an object is concerned only with the lexical and syntactic aspects of a path. The path does not necessarily exist in external storage, and the pathname is not necessarily valid for the current operating system or for a particular file system.
path constructorspath() noexcept;Default constructor.
Effects: Constructs an empty path.
Postconditions: |
|
Throws: |
Will not throw. |
path(path const & p);Copy constructor.
Effects: Constructs a copy of p.
Parameters: |
|
||
Postconditions: |
|
||
Throws: |
std::bad_alloc on memory allocation failure. |
path(path const & p, codecvt_type const & cvt);Copy constructor.
Effects: As if path(p).
![]() |
Note |
|---|---|
The |
path(path && p) noexcept;Move constructor.
Effects: Move-constructs from p.
Parameters: |
|
||
Postconditions: |
|
||
Throws: |
Will not throw. |
path(path && p, codecvt_type const & cvt) noexcept;Move constructor.
Effects: As if path(std::move(p)).
![]() |
Note |
|---|---|
The |
template<typename Source> path(Source const & source);Initializing constructor.
Effects: As if path(source, path::codecvt()).
template<typename Source> explicit path(Source const & source, codecvt_type const & cvt);Initializing constructor.
Effects: Constructs a path from source. Uses cvt facet to perform character code conversion, if needed.
Parameters: |
|
||||
Requires: |
|
||||
Throws: |
std::bad_alloc on memory allocation failure. boost::system::system_error in case of character code conversion errors. |
path(string_type && s) noexcept;Initializing constructor.
Effects: Constructs a path by moving from s.
Parameters: |
|
||
Postconditions: |
|
||
Throws: |
Will not throw. |
path(string_type && s, codecvt_type const & cvt) noexcept;Initializing constructor.
Effects: As if path(std::move(s)).
![]() |
Note |
|---|---|
The |
template<typename InputIterator> path(InputIterator begin, InputIterator end);Initializing constructor.
Effects: As if path(begin, end, path::codecvt()).
template<typename InputIterator> path(InputIterator begin, InputIterator end, codecvt_type const & cvt);Initializing constructor.
Effects:
Constructs a path from a range of characters denoted by begin and end. Uses cvt facet to perform character code conversion, if needed.
Parameters: |
|
||||||
Requires: |
|
||||||
Throws: |
std::bad_alloc on memory allocation failure. boost::system::system_error in case of character code conversion errors. |
path(std::nullptr_t) = delete;Constructor from null pointers is disabled.
path assignmentpath & operator=(path const & p);Copy assignment.
Returns: |
|
path & assign(path const & p);Copy assignment.
Effects: Copies the path p into *this.
Parameters: |
|
||
Postconditions: |
|
||
Returns: |
|
||
Throws: |
std::bad_alloc on memory allocation failure. |
path & assign(path const & p, codecvt_type const & cvt);Copy assignment.
![]() |
Note |
|---|---|
The |
Returns: |
|
path & operator=(path && p) noexcept;Move assignment.
Returns: |
|
path & assign(path && p) noexcept;Move assignment.
Effects: Move-assigns the path p to *this.
Parameters: |
|
||
Postconditions: |
|
||
Returns: |
|
||
Throws: |
Will not throw. |
path & assign(path && p, codecvt_type const & cvt) noexcept;Move assignment.
![]() |
Note |
|---|---|
The |
Returns: |
|
path & operator=(string_type && s) noexcept;Move-assigns characters in the string to the path.
Returns: |
|
path & assign(string_type && s) noexcept;Move-assigns characters in the string to the path.
Effects: Move-assigns the string s to *this.
Parameters: |
|
||
Postconditions: |
|
||
Returns: |
|
||
Throws: |
Will not throw. |
path & assign(string_type && s, codecvt_type const & cvt) noexcept;Move-assigns characters in the string to the path.
![]() |
Note |
|---|---|
The |
Returns: |
|
template<typename Source> path & operator=(Source const & source);Assigns characters in
source to the path.
Returns: |
|
template<typename Source> path & assign(Source const & source);Assigns characters in
source to the path.
Returns: |
|
template<typename Source> path & assign(Source const & source, codecvt_type const & cvt);Assigns characters in
source to the path. Effects:
Assigns source to *this. Uses cvt facet to perform character code conversion, if needed.
Parameters: |
|
||||
Requires: |
|
||||
Returns: |
|
||||
Throws: |
std::bad_alloc on memory allocation failure. boost::system::system_error in case of character code conversion errors. |
template<typename InputIterator> path & assign(InputIterator begin, InputIterator end);Assigns characters in the iterator range to the path.
Returns: |
|
template<typename InputIterator> path & assign(InputIterator begin, InputIterator end, codecvt_type const & cvt);Assigns characters in the iterator range to the path.
Effects:
Assigns a range of characters denoted by begin and end to *this. Uses cvt facet to perform character code conversion, if needed.
Parameters: |
|
||||||
Requires: |
|
||||||
Returns: |
|
||||||
Throws: |
std::bad_alloc on memory allocation failure. boost::system::system_error in case of character code conversion errors. |
path & operator=(std::nullptr_t) = delete;Assignment from null pointers is disabled.
path concatenationtemplate<typename Char> path & operator+=(Char c);Concatenates character
c to the end of the path.
Requires: |
|
Returns: |
|
path & operator+=(path const & p);Concatenates characters from
p to the end of the path.
Returns: |
|
path & concat(path const & p);Concatenates characters from
p to the end of the path. Effects: Copies the path p to the end of *this.
Parameters: |
|
||
Returns: |
|
||
Throws: |
std::bad_alloc on memory allocation failure. |
path & concat(path const & p, codecvt_type const & cvt);Concatenates characters from
p to the end of the path.
![]() |
Note |
|---|---|
The |
Returns: |
|
template<typename Source> path & operator+=(Source const & source);Concatenates characters from
source to the end of the path.
Returns: |
|
template<typename Source> path & concat(Source const & source);Concatinates characters from
source to the end of the path.
Returns: |
|
template<typename Source> path & concat(Source const & source, codecvt_type const & cvt);Concatinates characters from
source to the end of the path. Effects:
Concatenates the characters from source to the end of *this. Uses cvt facet to perform character code conversion, if needed.
Parameters: |
|
||||
Requires: |
|
||||
Returns: |
|
||||
Throws: |
std::bad_alloc on memory allocation failure. boost::system::system_error in case of character code conversion errors. |
template<typename InputIterator> path & concat(InputIterator begin, InputIterator end);Concatinates characters from the iterator range to the end of the path.
Returns: |
|
template<typename InputIterator> path & concat(InputIterator begin, InputIterator end, codecvt_type const & cvt);Concatinates characters from the iterator range to the end of the path.
Effects:
Concatenates a range of characters denoted by begin and end to the end of *this. Uses cvt facet to perform character code conversion, if needed.
Parameters: |
|
||||||
Requires: |
|
||||||
Returns: |
|
||||||
Throws: |
std::bad_alloc on memory allocation failure. boost::system::system_error in case of character code conversion errors. |
path appendingpath & operator/=(path const & p);Appends
p to the end of the path.
Returns: |
|
path & append(path const & p);Appends
p to the end of the path. Effects:
v3:
Concatenates path::preferred_separator to *this, converting format and encoding if required, unless:
an added separator would be redundant, or
would change a relative path to an absolute path, or
p.empty(), or
*p.native().cbegin() is a directory separator.
Then concatenates p.native() to *this.
v4:
If p.is_absolute() || (p.has_root_name() && p.root_name() != this->root_name()), assigns p to *this. Otherwise, modifies *this as if by these steps:
If p.has_root_directory(), removes root directory and relative path, if any.
Let x be a path with contents of p without a root name. If this->has_filename() is true and x
does not start with a directory separator, concatenates path::preferred_separator.
Concatenates x.native().
![]() |
Note |
|---|---|
Whether the path is absolute or not depends on the target OS conventions. Because of this, the result of append operation may be different for different operating systems for some paths. For example, |
Parameters: |
|
||
Returns: |
|
||
Throws: |
std::bad_alloc on memory allocation failure. |
path & append(path const & p, codecvt_type const &);Appends
p to the end of the path.
![]() |
Note |
|---|---|
The |
Returns: |
|
template<typename Source> path & operator/=(Source const & source);Appends
source to the end of the path.
Returns: |
|
template<typename Source> path & append(Source const & source);Appends
source to the end of the path.
Returns: |
|
template<typename Source> path & append(Source const & source, codecvt_type const & cvt);Appends
source to the end of the path.
Returns: |
|
template<typename InputIterator> path & append(InputIterator begin, InputIterator end);Appends characters from the iterator range to the end of the path.
Returns: |
|
template<typename InputIterator> path & append(InputIterator begin, InputIterator end, const codecvt_type & cvt);Appends characters from the iterator range to the end of the path.
Returns: |
|
path modifiersvoid clear() noexcept;Clears the path.
Postconditions: |
|
Throws: |
Will not throw. |
path & make_preferred();Converts directory separators to
path::preferred_separator.
Returns: |
|
path & remove_filename();Removes the trailing filename.
Effects:
v3:
As if *this = parent_path().
![]() |
Note |
|---|---|
This function is needed to efficiently implement |
v4:
Removes the filename() path element.
![]() |
Note |
|---|---|
Unlike v3, the trailing directory separator(s) are not removed. |
Returns: |
|
path & remove_filename_and_trailing_separators();Removes the trailing filename and directory separator.
Effects: As if *this = parent_path().
![]() |
Note |
|---|---|
This function is similar to |
Returns: |
|
path & remove_trailing_separator();Removes the trailing directory separator.
Effects:
If *this ends with a directory separator, removes that separator. Otherwise, keeps *this unmodified.
Returns: |
|
path & replace_filename(path const & replacement);Replaces the trailing filename.
Effects: As if remove_filename().append(replacement).
See Also: remove_filename, append.
Parameters: |
|
||
Returns: |
|
path & replace_extension(path const & new_extension = path());Replaces extension in the trailing filename.
Effects:
Any existing extension() is removed from the stored path, then
iff new_extension is not empty and does not begin with a dot character, a dot character is concatenated to the stored path, then
new_extension is concatenated to the stored path.
Parameters: |
|
||
Returns: |
|
void swap(path & rhs) noexcept;Swaps
*this and rhs path objects.
Parameters: |
|
||
Throws: |
Will not throw. |
path observersbool empty() const noexcept;
Returns: |
|
Throws: |
Will not throw. |
bool filename_is_dot() const;
Example:
std::cout << path(".").filename_is_dot(); // outputs 1 std::cout << path("/.").filename_is_dot(); // outputs 1 std::cout << path("foo/.").filename_is_dot(); // outputs 1 std::cout << path("foo/").filename_is_dot(); // v3 outputs 1, v4 outputs 0 std::cout << path("/").filename_is_dot(); // outputs 0 std::cout << path("/foo").filename_is_dot(); // outputs 0 std::cout << path("/foo.").filename_is_dot(); // outputs 0 std::cout << path("..").filename_is_dot(); // outputs 0
See the last bullet item in the path iterators forward traversal order list for why path("foo/").filename() is a dot filename in v3.
Returns: |
|
bool filename_is_dot_dot() const;
Returns: |
|
bool has_root_path() const;
Returns: |
|
bool has_root_name() const;
Returns: |
|
bool has_root_directory() const;
Returns: |
|
bool has_relative_path() const;
Returns: |
|
bool has_parent_path() const;
Returns: |
|
bool has_filename() const;
Returns: |
|
bool has_stem() const;
Returns: |
|
bool has_extension() const;
Returns: |
|
bool is_relative() const;
Returns: |
|
bool is_absolute() const;
![]() |
Note |
|---|---|
On POSIX systems, a path is considered absolute if it has a |
See Also: root_path, root_name, root_directory.
Returns: |
|
string_type const & native() const noexcept;
Returns: |
The pathname in the native format. |
const value_type * c_str() const noexcept;
string_type::size_type size() const noexcept;
template<typename String> String string() const;
Returns: |
|
template<typename String> String string(codecvt_type const & cvt) const;
Parameters: |
|
||
Requires: |
|
||
Returns: |
Pathname returned by |
||
Throws: |
std::bad_alloc on memory allocation failure. boost::system::system_error in case of character code conversion errors. |
std::string string() const;
Returns: |
|
std::string string(codecvt_type const & cvt) const;
Returns: |
|
std::wstring wstring() const;
Returns: |
|
std::wstring wstring(codecvt_type const & cvt) const;
Returns: |
|
path generic_path() const;
Returns: |
The pathname in the generic format. |
template<typename String> String generic_string() const;
Returns: |
|
template<typename String> String generic_string(codecvt_type const & cvt) const;
Parameters: |
|
||
Requires: |
|
||
Returns: |
Pathname returned by |
||
Throws: |
std::bad_alloc on memory allocation failure. boost::system::system_error in case of character code conversion errors. |
std::string generic_string() const;
Returns: |
|
std::string generic_string(codecvt_type const & cvt) const;
Returns: |
|
std::wstring generic_wstring() const;
Returns: |
|
std::wstring generic_wstring(codecvt_type const & cvt) const;
Returns: |
|
path comparisonint compare(path const & p) const;
![]() |
Note |
|---|---|
The elements are determined as if by iteration over the half-open range [ |
Returns: |
A value less than 0 if the elements of |
template<typename Source> int compare(Source const & source) const;
Returns: |
|
template<typename Source> int compare(Source const & source, codecvt_type const & cvt) const;
Returns: |
|
path decompositionpath root_path() const;
Returns: |
path root_name() const;
Returns: |
|
path root_directory() const;
Returns: |
|
path relative_path() const;
Returns: |
A |
path parent_path() const;Returns the path without the last component.
Example:
std::cout << path("/foo/bar.txt").parent_path(); // outputs "/foo" std::cout << path("/foo/bar").parent_path(); // outputs "/foo" std::cout << path("/foo/bar/").parent_path(); // outputs "/foo/bar" std::cout << path("/").parent_path(); // outputs "" std::cout << path(".").parent_path(); // outputs "" std::cout << path("..").parent_path(); // outputs ""
See the last bullet item in the path iterators forward traversal order list for why the "/foo/bar/" example doesn't output "/foo".
path filename() const;Returns the last filename component of the path.
Example:
std::cout << path("/foo/bar.txt").filename(); // outputs "bar.txt" std::cout << path("/foo/bar").filename(); // outputs "bar" std::cout << path("/foo/bar/").filename(); // v3 outputs "." // v4 outputs "" std::cout << path("/").filename(); // v3 outputs "/" // v4 outputs "" std::cout << path(".").filename(); // outputs "." std::cout << path("..").filename(); // outputs ".."
See the last bullet item in the path iterators forward traversal order list for why the "/foo/bar/" example doesn't output "bar".
Returns: |
v3: |
path stem() const;Returns the last filename component of the path without extension.
Example:
std::cout << path("/foo/bar.txt").stem() << '\\n'; // outputs "bar" std::cout << path(".hidden").stem() << '\\n'; // v3 outputs "" // v4 outputs ".hidden" path p = "foo.bar.baz.tar"; for (; !p.extension().empty(); p = p.stem()) // outputs: .tar std::cout << p.extension() << '\\n'; // .baz // .bar
Returns: |
If |
path extension() const;Returns extension of the last filename component of the path.
![]() |
Note |
|---|---|
Implementations are permitted but not required to define additional behavior for file systems which append additional elements to extensions, such as alternate data streams or partitioned dataset names. |
Example:
std::cout << path("/foo/bar.txt").extension(); // outputs ".txt"
![]() |
Note |
|---|---|
The dot is included in the return value so that it is possible to distinguish between no extension and an empty extension. See https://lists.boost.org/Archives/boost/2010/02/162028.php for more extensive rationale. |
Returns: |
The substring of |
path lexical operationspath lexically_normal() const;
Overview:
Returns *this with redundant current directory ("."), parent directory (".."), and directory separator elements removed.
![]() |
Note |
|---|---|
Uses |
Example:
std::cout << path("foo/./bar/..").lexically_normal() << std::endl; // outputs "foo" std::cout << path("foo/.///bar/../").lexically_normal() << std::endl; // v3: outputs "foo/." // v4: outputs "foo/"
On Windows, the returned path's directory-separator characters will be backslashes rather than slashes, but that does not affect path equality.
Returns: |
|
path lexically_relative(path const & base) const;
Overview:
Returns *this made relative to base. Treats empty or identical paths as corner cases, not errors. Does not resolve symlinks. Does not first normalize *this or base.
![]() |
Note |
|---|---|
Uses |
Example:
assert(path("/a/d").lexically_relative("/a/b/c") == "../../d"); assert(path("/a/b/c").lexically_relative("/a/d") == "../b/c"); assert(path("a/b/c").lexically_relative("a") == "b/c"); assert(path("a/b/c").lexically_relative("a/b/c/x/y") == "../.."); assert(path("a/b/c").lexically_relative("a/b/c") == "."); assert(path("a/b").lexically_relative("c/d") == "");
The above assertions will succeed. On Windows, the returned path's directory-separator characters will be backslashes rather than forward slashes, but that does not affect path equality.
![]() |
Note |
|---|---|
If symlink following semantics are desired, use the operational function |
![]() |
Note |
|---|---|
If normalization is needed to ensure consistent matching of elements, apply |
Parameters: |
|
||
Returns: |
|
path lexically_proximate(path const & base) const;
![]() |
Note |
|---|---|
If symlink following semantics are desired, use the operational function |
![]() |
Note |
|---|---|
If normalization is needed to ensure consistent matching of elements, apply |
See Also: lexically_relative.
Returns: |
If |
path iteratorsiterator begin() const;
Returns: |
An iterator for the first element in forward traversal order. If no elements are present, the end iterator. |
iterator end() const;
Returns: |
The end iterator in the forward traversal order. |
reverse_iterator rbegin() const;
Returns: |
An iterator for the first element in backward traversal order. If no elements are present, the end iterator. |
reverse_iterator rend() const;
Returns: |
The end iterator in the backward traversal order. |
path locale operationsstatic std::locale imbue(std::locale const & loc);
Effects:
Stores a copy of loc as the imbued path locale.
![]() |
Note |
|---|---|
The initial value of the imbued |
Returns: |
The previous imbued |
static codecvt_type const & codecvt();
Returns: |
The |
path
public
public data membersstatic value_type separator;
Character used to separate path elements in generic paths.
static value_type preferred_separator;
Character used to separate path elements in native paths.
This character may be the same as separator or different and depends on the target platform conventions. It is "/" on POSIX systems and "\" on Windows.
static value_type dot;
Character used to separate file name and extension.
It is "." on POSIX systems and Windows.