在线文档教程
C++
文件系统 | Filesystem

std::filesystem::path::filename

文件系统::path::filename

path filename() const;(since C++17)

返回路径的文件名组件。

相当于empty() ? path() : *--end()...

参数

%280%29

返回值

路径标识的文件名。

例外

%280%29

二次

#include <iostream> #include <filesystem> namespace fs = std::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'; }

二次

产出:

二次

"bar.txt" ".bar" "." "." ".." "." ".." "/"

二次

另见

extensionreturns the file extension path component (public member function)
stemreturns the stem path component (public member function)
replace_filenamereplaces the last path component with another path (public member function)
has_filenamechecks if the corresponding path element is not empty (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/file system/path/filename