std::filesy...ce_filename(3)
π NAME π
std::filesystem::directory_entry::replace_filename β like, that's the whole name, it's a member function of std::filesystem::directory_entry and it lives in the C++ Standard Library, bestie. It swaps out the filename of a directory entry. Obsessed.
β¨ SYNOPSIS β¨
void replace_filename( const std::filesystem::path& p );
(1) (since C++17)
void replace_filename( const std::filesystem::path& p,
std::error_code& ec );
(2) (since C++17)
π¦ DESCRIPTION π¦
Okay so what this does: it changes the filename of the directory entry. β¨
Effectively, it modifies the path member by calling path.replace_filename(p), and then it calls refresh to update the cached attributes. Lowkey important: if an error occurs, the values of the cached attributes are unspecified. π
Heads up bestie: this function does not commit any changes to the filesystem. It's giving "vibes only, no actual disk writes." π
π PARAMETERS π
pβ the path to append to the parent path of the currently stored path.ecβ out-parameter for error reporting in the non-throwing overload.
β RETURN VALUE β
None. Nothing. Nada. (none) π«Ά
π₯ EXCEPTIONS π₯
Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails. π
- Throws
std::filesystem::filesystem_erroron underlying OS API errors, constructed withpas the first path argument and the OS error code as the error code argument. - Sets a
std::error_code¶meter to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur.
πΈ EXAMPLE πΈ
// Run this code
#include <filesystem>
#include <iostream>
int main()
{
namespace fs = std::filesystem;
{
fs::directory_entry entry{"alpha"};
std::cout << entry << '\n';
entry.replace_filename("omega");
std::cout << entry << '\n';
}
{
fs::directory_entry entry{"/alpha/"};
std::cout << entry << '\n';
entry.replace_filename("omega");
std::cout << entry << '\n';
}
}
π Output π
"alpha"
"omega"
"/alpha/"
"/alpha/omega"
π SEE ALSO π
assignβ assigns contents (public member function)replace_filenameβ replaces the last path component with another path (public member function ofstd::filesystem::path)
and that's the whole page, bestie β go forth and replace filenames responsibly π§π
Sign-off time π: this iconic little page comes from cppreference.com, dated 2024.06.10 β std::filesy...ce_filename(3), the manual entry that ate and left no crumbs. π
πΈ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms