path::filename Method

Retrieves the right-most path element, which might be a file or a directory, or a volume. If the path ends in a non-root trailing slash, then dot (.) is returned.

string_type filename() const;

Return Value

This method returns empty() ? string_type() : *--end().

  • If the relative path is present in the stored file name, the return value is the last path element.

  • Otherwise, if the root is present, the return value is the root.

  • Otherwise, if the prefix is present, the return value is the prefix.

  • Otherwise, the path is empty, and the return value is also empty path.

Remarks

A stored file name has an optional prefix, followed by an optional root, followed by an optional relative path. The <filesystem> types and functions support the UNIX-based file system convention in which directories and volumes are also considered files.

This function looks at the path object, not an actual file system entry. There is no deterministic way to tell, just by examining a path, whether the last element refers to a directory, or to a file that has no extension. In a path that ends in \foo\bar, bar might signify either a file or a directory. When you examine files that exist in the file system, you can use is_regular_file or is_directory to determine whether the last element in a path is a file name or a directory name.

The prefix is the elements up to and including the first colon.

The root is a forward slash or backslash.

The relative path is one or more path elements that are delimited by forward slashes or backslashes.

Requirements

Header: filesystem

Namespace: std::tr2::sys

See Also

Reference

path Class (C++ Standard Template Library)

<filesystem>