std::filesystem::directory_entry::path
來自 cppreference.com
< cpp | filesystem | directory_entry
const std::filesystem::path& path() const noexcept; |
(C++17 起) | |
operator const std::filesystem::path& () const noexcept; |
(C++17 起) | |
返回目錄項所指向的完整路徑。
目錄 |
[編輯] 引數
(無)
[編輯] 返回值
目錄項所指向的完整路徑。
[編輯] 示例
執行此程式碼
#include <filesystem> #include <fstream> #include <iostream> namespace fs = std::filesystem; std::string get_stem(const fs::path& p) { return p.stem().string(); } void create_file(const fs::path& p) { std::ofstream o{p}; } int main() { const fs::path dir{"tmp_dir"}; fs::create_directory(dir); create_file(dir / "one"); create_file(dir / "two"); create_file(dir / "three"); for (const auto& file : fs::directory_iterator(dir)) { // Explicit conversion std::cout << get_stem(file.path()) << '\n'; // Implicit conversion std::cout << get_stem(file) << '\n'; } fs::remove_all(dir); }
可能的輸出
two two one one three three
[編輯] 參閱
(C++17) |
表示一個路徑 (類) |