简体   繁体   English

C ++ proc_open模拟

[英]C++ proc_open analogue

There's a handy function in PHP called proc_open . PHP中有一个方便的函数,称为proc_open It could be used to call an executable, opening its stdin , stdout and stderr as pipes. 它可以用来调用可执行文件,将其stdinstdoutstderr作为管道打开。

Is there a good cross-platform version of this function in C++? C ++中是否有此函数的跨平台版本? The only googlable thing is this tutorial for Windows (though code from it just hangs). 唯一可以谷歌浏览的东西是 Windows教程(尽管其中的代码只是挂起)。

You could probably get 'somewhere' with 您可能会与“某处”

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. 如我所见,Boost.Process不再处于活动开发中,并且示例未使用当前版本(1.54)进行编译,也不是当前版本(1.4x-在升级Boost之前我记下了确切的版本)版本的提升,所以我需要撤消我的建议。

Original post 原始帖子

There is Boost.Process library which you could use. 有可以使用的Boost.Process库。 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; 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM