W3cubDocs

/C++

std::filesystem::path::stem

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:

If the first character in the filename is a period, that period is ignored (a filename like ".profile" is not treated as an extension).

If the filename is one of the special filesystem components dot or dot-dot, or if it has no periods, the function returns the entire filename().

Parameters

(none).

Return value

The stem of the filename identified by the path (i.e. the filename without the final extension).

Exceptions

(none).

Example

#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
 
int main()
{
    std::cout << fs::path("/foo/bar.txt").stem() << '\n'
              << fs::path("/foo/.bar").stem() << '\n';
 
    for (fs::path p = "foo.bar.baz.tar"; !p.extension().empty(); p = p.stem())
        std::cout << p.extension() << '\n';
}

Output:

"bar"
".bar"
".tar"
".baz"
".bar"

See also

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.
http://en.cppreference.com/w/cpp/filesystem/path/stem