在C++10中,可以使用filesystem库来获取路径的父目录。

下面是一个示例代码:

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main() {
    fs::path filePath = "/path/to/file.txt";
    fs::path parentPath = filePath.parent_path();

    std::cout << "Parent directory: " << parentPath << std::endl;

    return 0;
}

在上面的代码中,filePath表示要获取父目录的路径,parentPath表示父目录的路径。通过调用parent_path()函数,可以获取到路径的父目录。

在这个示例中,输出结果将会是/path/to,表示文件file.txt的父目录。

需要注意的是,在使用filesystem库之前,需要确保编译器支持C++17标准,并链接对应的库。在使用GCC编译器时,需要加上-lstdc++fs选项


原文地址: https://www.cveoy.top/t/topic/iF8e 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录