【问题标题】:C++ proc_open analogueC++ proc_open 模拟
【发布时间】:2013-09-15 23:29:56
【问题描述】:

PHP 中有一个方便的函数,称为proc_open。它可用于调用可执行文件,将其stdinstdoutstderr 作为管道打开。

这个函数在 C++ 中有没有好的跨平台版本?唯一可以谷歌搜索的是this Windows 教程(尽管其中的代码只是挂起)。

【问题讨论】:

    标签: php c++ cross-platform popen stdio


    【解决方案1】:

    你可能会得到“某处”

    【讨论】:

    • 自 2006 年以来,Boost 进程似乎还没有出现在 Boost 中。
    • 我知道这一点,这就是我提到它的原因。这样命名有点混乱
    【解决方案2】:

    编辑:

    正如我所看到的,Boost.Process 不再处于积极开发中,并且示例没有使用当前 (1.54) 进行编译,也不是最新 (1.4x - 我在升级之前忘记写下确切的版本) boost) 版本的 boost,所以我需要撤回我的建议。

    原帖

    您可以使用Boost.Process 库。你可以找到很好的例子here。另外,检查this chapterhere,以及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; 
    } 
    

    【讨论】:

      猜你喜欢
      • 2012-04-23
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 2020-04-29
      • 2011-03-04
      • 2018-10-22
      • 2011-11-10
      相关资源
      最近更新 更多