【问题标题】:c++ get directory path, parse folder name, and print file content from parent directoryc++ 从父目录获取目录路径,解析文件夹名称,打印文件内容
【发布时间】:2017-05-04 20:10:21
【问题描述】:

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

/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,导航到父目录并从名为 node.xy 的文件中读取。

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

谢谢

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    Boost 文件系统有丰富的方法。有很多方法可以做同样的事情。始终将文档放在手边,您将需要它们。

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 2019-09-27
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      相关资源
      最近更新 更多