std::filesystem::path::append, std::filesystem::path::operator/=
From cppreference.com
                    
                                        
                    < cpp | filesystem | path
                    
                                                            
                    | path& operator/=(const path& p); | (1) | (since C++17) | 
| template< class Source > path& operator/=( const Source& source ); | (2) | (since C++17) | 
| template< class Source > path& append( const Source& source ); | (3) | (since C++17) | 
| template< class InputIt > path& append( InputIt first, InputIt last ); | (4) | (since C++17) | 
1) First, appends the preferred directory separator to this, except if any of the following conditions is true:
 * the separator would be redundant (*this already ends with a separator)
 * *this is empty, or adding it would turn a relative path to an absolute path in some other way
 * 
p is an empty path. * p.native() begins with a directory separator.
 Then, appends p.native() to the pathname maintained by *this
 The behavior is undefined if p.has_root_name() is true.
2,3) Same as (1), but accepts any std::basic_string, std::basic_string_view, null-terminated multicharacter string, or an input iterator pointing to a null-terminated multicharacter sequence. Equivalent to return operator/=(path(source));.
4) Same as (1), but accepts any iterator pair that designates a multicharacter string. Equivalent to return operator/=(path(first, last));
Parameters
| p | - | pathname to append | 
| source | - | std::basic_string, std::basic_string_view, null-terminated multicharacter string, or an input iterator pointing to a null-terminated multicharacter sequence, which represents a path name (either in portable or in native format) | 
| first, last | - | pair of InputIterators that specify a multicharacter sequence that represents a path name | 
| Type requirements | ||
| - InputItmust meet the requirements ofInputIterator. | ||
| -The value type of InputItmust be one of the encoded character types (char, wchar_t, char16_t and char32_t) | ||
Return value
*this
Exceptions
May throw filesystem_error on underlying OS API errors or std::bad_alloc if memory allocation fails.
Example
Run this code
#include <iostream> #include <filesystem> namespace fs = std::filesystem; int main() { fs::path p1 = "C:"; p1 /= "Users"; // does not insert a separator std::cout << "\"C:\" / \"Users\" == " << p1 << '\n'; p1 /= "batman"; // inserts fs::path::preferred_separator, '\' on Windows std::cout << "\"C:\" / \"Users\" / \"batman\" == " << p1 << '\n'; }
Possible output:
"C:" / "Users" == "C:Users" "C:" / "Users" / "batman" == "C:Users\batman"
See also
| concatenates two paths without introducing a directory separator (public member function) | |
| concatenates two paths with a directory separator (function) |