c ++从父目录

时间:2016-12-19 17:49:56

标签: c++ boost

在使用模拟器并打开节点内的终端时,我需要从父目录中读取以当前目录名开头的文件。结构如下:

/path/to/directory/session#/node.conf
                      |
                      |_node.xy

我可以使用boost库获取当前路径(以及父路径)/path/to/directory/session#/node.conf

std::string cwd = getcwd(NULL, 0);
boost::filesystem::path p1(cwd);
... p1.parent_path()

我不熟悉Boost,但我想获取文件夹名称node.conf,解析得到node,导航到父目录并从名为{的文件中读取{1}}。

最好的方法是什么?我在这里寻找其他问题,但找不到适合我的问题。

由于

1 个答案:

答案 0 :(得分:0)

Boost Filesystem丰富了方法。有很多方法可以做同样的事情。始终保持文档的方便,你需要它们。

Documentation Links

Reference Documentation

所以:

#include <iostream>
#include <boost/filesystem.hpp>

int main( )
{
    namespace bfs= boost::filesystem;

    bfs::path p1( "/path/to/directory/session#/node.conf" );
    bfs::path target( p1.parent_path( ) / "_node.xy" );
    std::cout << target << std::endl;
    //or
    bfs::path targ2( p1 );
    targ2.remove_filename( );
    targ2/= "_node.xy";
    std::cout << targ2 << std::endl;
    return 0;
}
相关问题