【问题标题】:How to get the absolute file path from the system PATH in C++?如何从 C++ 中的系统 PATH 获取绝对文件路径?
【发布时间】:2019-08-16 19:59:47
【问题描述】:

如果文件在系统 PATH 中,是否可以通过文件名获取文件的完整路径?

例如,我的 PATH 中有 cmake。我可以使用 C++ 文件系统库获取它的完整路径吗?我只想跨平台检查文件是否存在。

【问题讨论】:

标签: c++ path boost-filesystem


【解决方案1】:

Boost.Processsearch_path 中具有此功能。这是一个小的 linux 示例测试搜索路径,用于在系统路径中查找 sh 可执行文件的位置。

#include <iostream>
#include <cstdlib>
#include <boost/process/search_path.hpp>
#include <string>

int main()
{ 
    auto myPath = boost::process::search_path("sh");

    std::cout << "sh executable path " << myPath << std::endl;

}

要测试的实时环境: https://wandbox.org/permlink/c3TbKq4sliBjxRVc

输出:

开始
sh 路径 "/bin/sh"
0
完成

【讨论】:

    猜你喜欢
    • 2015-11-16
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多