Namespaces
Variants
Actions

std::filesystem::path::remove_filename

From cppreference.com
< cpp‎ | filesystem‎ | path
 
C++
 
Filesystem library
Classes
Functions
File types
 
std::filesystem::path
path& remove_filename();
(since C++17)

Removes a single generic-format filename component (as returned by filename) from the given generic-format path.

After this function completes, has_filename returns false.

Contents

[edit] Parameters

(none)

[edit] Return value

*this

[edit] Exceptions

May throw implementation-defined exceptions.

[edit] Example

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
 
int main()
{
    fs::path p;
    std::cout << std::boolalpha
              << (p = "foo/bar").remove_filename() << '\t' << p.has_filename() << '\n'
              << (p = "foo/").remove_filename() << '\t' << p.has_filename() << '\n'
              << (p = "/foo").remove_filename() << '\t' << p.has_filename() << '\n'
              << (p = "/").remove_filename() << '\t' << p.has_filename() << '\n'
              << (p = "").remove_filename() << '\t' << p.has_filename() << '\n';
}

Output:

"foo/"  false
"foo/"  false
"/"     false
"/"     false
""      false

[edit] See also

returns the filename path component
(public member function) [edit]
replaces the last path component with another path
(public member function) [edit]
checks if the corresponding path element is not empty
(public member function)

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant