std::filesy...e_extension(3)
πΈ NAME πΈ
okay bestie, welcome to std::filesy...e_extension(3) β this is the C++ Standard Libary page (yes, βLibaryβ, weβre keeping the spelling π
). The star of the show is std::filesystem::path::replace_extension - std::filesystem::path::replace_extension. Like, the original repeats it after the dash, and we respect the lore. π«Ά
β¨ Synopsis β¨
path& replace_extension( const path& replacement = path() ); (since C++17)
This one replaces the extension with replacement, or removes it when you use the default value of replacement. No cap. Hereβs the little ritual it does:
- First up, if this path has an
extension(), it gets removed from the generic-format view of the pathname. π§ - Then a dot character is appended to the generic-format view of the pathname, but only if
replacementis not empty and does not begin with a dot character. β - Then
replacementis appended as if byoperator+=(replacement). Slay. π
π Parameters π
replacementβ the extension to replace with. Thatβs the whole assignment, bestie.
π Return value π
*this β it literally returns itself. Main character energy, honestly. π
π Exceptions π
May throw implementation-defined exceptions. So like, itβs not giving βguaranteed safe napβ β implementation-defined means exactly that. π
π Notes π
The type of replacement is std::filesystem::path even though it is not intended to represent an object on the file system, in order to correctly account for the filesystem character encoding. iykyk β itβs a path type doing encoding homework, not a filesystem object.
π§ͺ Example π§ͺ
bestie, run this code:
// Run this code
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <utility>
int main()
{
const int width1{18}, width2{11}; // columns' width
std::cout << std::left << std::setw(width1) << "Path:"
<< std::setw(width2) << "Ext:" << "Result:\n";
for (const auto& [p, e] : {
std::make_pair("/foo/bar.jpg", ".png"),
{"/foo/bar.jpg", "png"},
{"/foo/bar.jpg", "."},
{"/foo/bar.jpg", ""},
{"/foo/bar.", "png"},
{"/foo/bar", ".png"},
{"/foo/bar", "png"},
{"/foo/bar", "."},
{"/foo/bar", ""},
{"/foo/.", ".png"},
{"/foo/.", "png"},
{"/foo/.", "."},
{"/foo/.", ""},
{"/foo/", ".png"},
{"/foo/", "png"}})
{
std::filesystem::path path{p}, ext{e};
std::cout << std::setw(width1) << path << std::setw(width2) <<
ext;
path.replace_extension(ext);
std::cout << path << '\n';
}
}
π₯οΈ Output π₯οΈ
Path: Ext: Result:
"/foo/bar.jpg" ".png" "/foo/bar.png"
"/foo/bar.jpg" "png" "/foo/bar.png"
"/foo/bar.jpg" "." "/foo/bar."
"/foo/bar.jpg" "" "/foo/bar"
"/foo/bar." "png" "/foo/bar.png"
"/foo/bar" ".png" "/foo/bar.png"
"/foo/bar" "png" "/foo/bar.png"
"/foo/bar" "." "/foo/bar."
"/foo/bar" "" "/foo/bar"
"/foo/." ".png" "/foo/..png"
"/foo/." "png" "/foo/..png"
"/foo/." "." "/foo/.."
"/foo/." "" "/foo/."
"/foo/" ".png" "/foo/.png"
"/foo/" "png" "/foo/.png"
π See also π
extensionβ returns the file extension path component (public member function)filenameβ returns the filename path component (public member function)stemβ returns the stem path component (filename without the final extension) (public member function)has_extensionβ checks if the corresponding path element is not empty (public member function)
thatβs the whole vibe, bestie π«Ά this page is from http://cppreference.com, dated 2024.06.10, and itβs the std::filesy...e_extension(3) page. mwah πβ¨
π¦ 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