# std::experimental::filesystem::path::filename

> C++ Atlas: `cppatlas.com/w/cpp/experimental/fs/path/filename` | Source: https://en.cppreference.com/w/cpp/experimental/fs/path/filename | Domain: Technical specifications

Returns the filename component of the path.

## Official Specification Summary (cppreference)

```cpp
path filename() const; // (filesystem TS)
```

Returns the filename component of the path.

Equivalent to `empty() ? path(): *--end()`.

### Parameters

(none)

### Return value

The filename identified by the path.

### Exceptions

May throw implementation-defined exceptions.

### Example

```cpp
#include <experimental/filesystem>
#include <iostream>
namespace fs = std::experimental::filesystem;

int main()
{
    std::cout << fs::path("/foo/bar.txt").filename() << '\n'
              << fs::path("/foo/.bar").filename() << '\n'
              << fs::path("/foo/bar/").filename() << '\n'
              << fs::path("/foo/.").filename() << '\n'
              << fs::path("/foo/..").filename() << '\n'
              << fs::path(".").filename() << '\n'
              << fs::path("..").filename() << '\n'
              << fs::path("/").filename() << '\n';
}
```

Output:

```text
"bar.txt"
".bar"
"."
"."
".."
"."
".."
"/"
```

### See also

- [`extension`](https://en.cppreference.com/w/cpp/experimental/fs/path/extension): returns the file extension path component (public member function)
- [`stem`](https://en.cppreference.com/w/cpp/experimental/fs/path/stem): returns the stem path component (public member function)
- [`replace_filename`](https://en.cppreference.com/w/cpp/experimental/fs/path/replace_filename): replaces the last path component with another path (public member function)
- [`has_filename`](https://en.cppreference.com/w/cpp/experimental/fs/path/has_path): checks if the corresponding path element is not empty (public member function)

---
Attribution: content from https://en.cppreference.com/w/cpp/experimental/fs/path/filename (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.