std::filesy...::extension(3)

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

πŸ’… 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