path stem() const; | (since C++17) |
Returns the filename identified by the generic-format path stripped of its extension.
Returns the substring from the beginning of filename() up to and not including the last period (.) character, with the following exceptions:
filename(). (none).
The stem of the filename identified by the path (i.e. the filename without the final extension).
May throw implementation-defined exceptions.
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
for (const fs::path p : {"/foo/bar.txt", "/foo/.bar", "foo.bar.baz.tar"})
std::cout << "path: " << p << ", stem: " << p.stem() << '\n';
std::cout << '\n';
for (fs::path p = "foo.bar.baz.tar"; !p.extension().empty(); p = p.stem())
std::cout << "path: " << p << ", extension: " << p.extension() << '\n';
}Output:
path: "/foo/bar.txt", stem: "bar" path: "/foo/.bar", stem: ".bar" path: "foo.bar.baz.tar", stem: "foo.bar.baz" path: "foo.bar.baz.tar", extension: ".tar" path: "foo.bar.baz", extension: ".baz" path: "foo.bar", extension: ".bar"
| returns the filename path component (public member function) |
|
| returns the file extension path component (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/filesystem/path/stem