- filesystem[meta header]
- std::filesystem[meta namespace]
- path[meta class]
- function[meta id-type]
- cpp17[meta cpp]
path filename() const;
パスが保持しているファイル名を取得する。
ファイル名とは、ディレクトリ区切り文字で分割された末尾要素のことを指す。そのため、末尾にディレクトリ区切り文字を含まないディレクトリ名もファイル名と見なされる。また、ルートディレクトリはファイル名とは見なされず、ルートパスのみが含まれる場合は空のパスが返る。
カレントディレクトリを表す「"."
(ドット x 1)」および親ディレクトリを表す「".."
(ドット x 2)」もまた、ファイル名と見なされる。
return relative_path().empty() ? path() : *--end();
- relative_path()[link relative_path.md]
- empty()[link empty.md]
- end()[link end.md]
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
fs::path ps[] = {
"/foo/bar.txt", // ファイル名を含むパス
"/foo/bar/", // ディレクトリパス
"/", // ルートパスのみ
".", // カレントディレクトリ
".." // 親ディレクトリ
};
for (const fs::path& p : ps) {
std::cout << p << " : " << p.filename() << std::endl;
}
}
- filename()[color ff0000]
"/foo/bar.txt" : "bar.txt"
"/foo/bar/" : ""
"/" : ""
"." : "."
".." : ".."
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
fs::path ps[] = {
"C:/foo/bar.txt", // ファイル名を含むパス
"C:/foo/bar/", // ディレクトリパス
"C:", // ルートディレクトリ
".", // カレントディレクトリ
".." // 親ディレクトリ
};
for (const fs::path& p : ps) {
std::cout << p << " : " << p.filename() << std::endl;
}
}
- filename()[color ff0000]
"C:/foo/bar.txt" : "bar.txt"
"C:/foo/bar/" : ""
"C:" : ""
"." : "."
".." : ".."
- C++17
- Clang:
- GCC: 8.1 [mark verified]
- Visual C++: 2017 Update 7 [mark verified]