簡體   English   中英

C ++ proc_open模擬

[英]C++ proc_open analogue

PHP中有一個方便的函數,稱為proc_open 它可以用來調用可執行文件,將其stdinstdoutstderr作為管道打開。

C ++中是否有此函數的跨平台版本? 唯一可以谷歌瀏覽的東西是 Windows教程(盡管其中的代碼只是掛起)。

您可能會與“某處”

編輯:

如我所見,Boost.Process不再處於活動開發中,並且示例未使用當前版本(1.54)進行編譯,也不是當前版本(1.4x-在升級Boost之前我記下了確切的版本)版本的提升,所以我需要撤消我的建議。

原始帖子

有可以使用的Boost.Process庫。 你可以在這里找到很好的例子。 此外,檢查這一章這里 ,還有這個

// 
// 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