首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::filesystem::path::filename

path filename() const;

(since C++17)

返回路径的文件名组件。

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

参数

%280%29

返回值

路径标识的文件名。

例外

%280%29

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
"bar.txt"
".bar"
"."
"."
".."
"."
".."
"/"

二次

另见

extension

returns the file extension path component (public member function)

stem

returns the stem path component (public member function)

replace_filename

replaces the last path component with another path (public member function)

has_filename

checks if the corresponding path element is not empty (public member function)

代码语言:txt
复制
 © cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券