std::filesy...::extension(3)
π
std::filesystem::path::extension β std::filesy...::extension(3)
okay so bestie, welcome to the C++ Standard Libary manual (yes that's literally how the manual name is spelled, i don't make the rules π«Ά). today's main character is std::filesystem::path::extension, and she is so much more than she looks. let's get into it β¨
πΈ Synopsis
path extension() const; (since C++17)
π Description
okay so what does she actually do? she returns the extension of the filename() component of the generic-format view of *this. that's the whole vibe check. π¦
here's the tea on how she picks it: if the filename() component of the generic-format path contains a period (.), and it's not one of the special filesystem elements dot or dot-dot, then the extension is the substring beginning at the rightmost period (period included, she's not giving it up) and running all the way until the end of the pathname. π₯
if the first character in the filename is a period, that period is ignored. so a filename like .profile is not treated as an extension β she said "dotfile β extension", periodt. π
and if the pathname is either . or .., or if filename() doesn't contain the '.' character at all, then you get an empty path back. no extension, no problem, moving on. β¨
heads up π additional behavior may be defined by the implementations for file systems which append additional elements (such as alternate data streams or partitioned dataset names) to extensions. iykyk, that's implementation territory, not our business.
π Parameters
(none) β she takes zero arguments, she's low maintenance, she's iconic π
π Return value
the extension of the current pathname, or an empty path if there's no extension. simple as that. πΈ
π¦ Exceptions
may throw implementation-defined exceptions. π so like, don't just assume she's chill 100% of the time, okay?
β Notes
the extension as returned by this function includes a period, and that's on purpose β it's so you can distinguish a file that ends with a period (function returns ".") from a file with no extension at all (function returns ""). literally the whole point of the period, bestie π
on a non-POSIX system, it is possible that:
p.stem() + p.extension() != p.filename()
even though the generic-format versions are the same. so like... don't be shocked, it's not a bug, it's just non-POSIX being non-POSIX π«
π₯ Example
// Run this code
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
std::cout << fs::path("/foo/bar.txt").extension() << '\n'
<< fs::path("/foo/bar.").extension() << '\n'
<< fs::path("/foo/bar").extension() << '\n'
<< fs::path("/foo/bar.txt/bar.cc").extension() << '\n'
<< fs::path("/foo/bar.txt/bar.").extension() << '\n'
<< fs::path("/foo/bar.txt/bar").extension() << '\n'
<< fs::path("/foo/.").extension() << '\n'
<< fs::path("/foo/..").extension() << '\n'
<< fs::path("/foo/.hidden").extension() << '\n'
<< fs::path("/foo/..bar").extension() << '\n';
}
Output:
".txt"
"."
""
".cc"
"."
""
""
""
""
".bar"
look at that output bestie, she ATE π
notice /foo/bar. gives you "." and /foo/bar gives you "" β that's the whole period thing from the Notes working exactly as advertised. and /foo/..bar gives ".bar" because the first char is a period so it gets ignored, but the rightmost period still counts. chef's kiss π
π See also
filenameβ returns the filename path component (public member function)stemβ returns the stem path component (filename without the final extension) (public member function)replace_extensionβ replaces the extension (public member function)has_extensionβ checks if the corresponding path element is not empty (public member function)
and that's the page, bestie π§ this absolute queen was brought to you by http://cppreference.com, dated 2024.06.10 β go drink some water and go call .extension() on something, you've earned it ππ
π¦ 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