dptgpyl61857413
2013-09-11 19:31 阅读 77
已采纳

C ++ proc_open analogue

There's a handy function in PHP called proc_open. It could be used to call an executable, opening its stdin, stdout and stderr as pipes.

Is there a good cross-platform version of this function in C++? The only googlable thing is this tutorial for Windows (though code from it just hangs).

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dqn48247 dqn48247 2013-09-11 19:33

    You could probably get 'somewhere' with

    点赞 8 评论 复制链接分享
  • duanan5940 duanan5940 2013-09-11 19:34

    Edit:

    As I can see, Boost.Process is no longer in the active development, and the examples are not compiling with the current (1.54) and not so current (1.4x - I forgot to write down the exact version before I upgraded boost) versions of boost, so I need to retract my recommendation.

    Original post

    There is Boost.Process library which you could use. You can find good example here. Also, check this chapter from here, as well as this.

    // 
    // Boost.Process 
    // ~~~~~~~~~~~~~ 
    // 
    // Copyright (c) 2006, 2007 Julio M. Merino Vidal 
    // Copyright (c) 2008 Boris Schaeling 
    // 
    // Distributed under the Boost Software License, Version 1.0. (See accompanying 
    // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 
    // 
    
    #include <boost/process.hpp> 
    #include <string> 
    #include <vector> 
    #include <iostream> 
    
    namespace bp = ::boost::process; 
    
    bp::child start_child() 
    { 
        std::string exec = "bjam"; 
    
        std::vector<std::string> args; 
        args.push_back("--version"); 
    
        bp::context ctx; 
        ctx.stdout_behavior = bp::capture_stream(); 
    
        return bp::launch(exec, args, ctx); 
    } 
    
    int main() 
    { 
        bp::child c = start_child(); 
    
        bp::pistream &is = c.get_stdout(); 
        std::string line; 
        while (std::getline(is, line)) 
            std::cout << line << std::endl; 
    } 
    
    点赞 9 评论 复制链接分享

相关推荐