std::filesystem::path::parent_path
来自cppreference.com
< cpp | filesystem | path
path parent_path() const; |
(C++17 起) | |
返回到亲目录的路径。
若 has_relative_path() 返回 false ,则结果是 *this 的副本。否则结果是通用格式路径名为 *this 的通用路径名在其迭代中产生少一个元素的最长前缀。
参数
(无)
返回值
到亲目录的路径。
异常
(无)
示例
运行此代码
#include <iostream> #include <filesystem> namespace fs = std::filesystem; int main() { for(fs::path p : {"/var/tmp/example.txt", "/", "/var/tmp/."}) std::cout << "The parent path of " << p << " is " << p.parent_path() << '\n'; }
可能的输出:
The parent path of "/var/tmp/example.txt" is "/var/tmp" The parent path of "/" is "/" The parent path of "/var/tmp/." is "/var/tmp"