std::filesystem::path::replace_filename
From cppreference.com
< cpp | filesystem | path
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
std::filesystem::path
path::replace_filename
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
path& replace_filename( const path& replacement );
(since C++17)
Replaces a single filename component with replacement.
Equivalent to: remove_filename(); return operator/=(replacement);.
[edit] Parameters
replacement
-
path
used for replacing the filename component
[edit] Return value
*this
[edit] Exceptions
May throw implementation-defined exceptions.
[edit] Example
Run this code
#include <filesystem> #include <iostream> namespace fs = std::filesystem; int main() { std::cout << fs::path("/foo").replace_filename("bar") << '\n' << fs::path("/").replace_filename("bar") << '\n' << fs::path("").replace_filename("pub") << '\n'; }
Output:
"/bar" "/bar" "pub"