std::filesy...ace_filename(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME

okay so bestie, welcome to the std::filesystem::path::replace_filename glow-up page ✨ this is straight out of the C++ Standard Libary manual β€” yes that's literally the manual name, i don't make the rules β€” and it's filed under std::filesy...ace_filename(3) if you're searching for it πŸŽ€

std::filesystem::path::replace_filename β€” std::filesystem::path::replace_filename (yes the name is the whole name, she said what she said πŸ’…)


🌸 SYNOPSIS

path& replace_filename( const path& replacement );  (since C++17)

so what does she actually do? she replaces a single filename component with replacement πŸ¦‹ that's the whole vibe, no cap.

it's literally equivalent to this little two-step:

remove_filename(); return operator/=(replacement);

like she removes the old filename, then slaps the new one on with /= β€” iconic, efficient, ate πŸ’–


πŸŽ€ PARAMETERS

  • replacement β€” the path you're using to replace the filename component with 🫢 that's it, that's the whole parameter.

✨ RETURN VALUE

*this β€” she returns herself πŸ’… main character energy fr fr.


πŸ”₯ EXCEPTIONS

may throw implementation-defined exceptions 😭 so like, don't @ her if your particular stdlib decides to be dramatic. it's giving "your mileage may vary" but make it C++.


🌈 EXAMPLE

go ahead and run this code, bestie πŸ‘‘

#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"

look at that β€” "/foo" becomes "/bar", the root "/" also becomes "/bar", and the empty path becomes "pub" πŸ¦‹ three little glow-ups, all slaying πŸ’œ


πŸ‘‘ SEE ALSO

  • replace_extension β€” replaces the extension (public member function) πŸŽ€
  • filename β€” returns the filename path component (public member function) 🌸
  • remove_filename β€” removes filename path component (public member function) πŸ’€
  • has_filename β€” checks if the corresponding path element is not empty (public member function) πŸ™ˆ

ngl bestie, always double-check your implementation-defined exceptions before you ship β€” she's cute but she's not promising you anything 😭

that's the whole page, periodt πŸ’…βœ¨ sourced from http://cppreference.com, dated 2024.06.10 β€” mwah, go write some filesystem code, you've got this πŸ«ΆπŸŽ€πŸ¦‹

πŸ“¦ where this page comes from