C++ 庫:Libexecstream

jopen 10年前發布 | 11K 次閱讀 C/C++開發 Libexecstream

Libexecstream 是 C++ 庫,允許你運行一個子進程并且獲取進程的輸入,輸出和錯誤,類似標準 C++ 流。

示例:

#include <exec-stream.h>
#include <string>
...
try {
    exec_stream_t es( "perl", "" ); // run perl without any arguments 
    es.in() << "print \"hello world\";"; // and make it print "hello world" 
    es.close_in();                        // after the input was closed 
    std::string hello, world;
    es.out() >> hello; // read the first word of output 
    es.out() >> world; // read the second word }catch( std::exception const & e ) {
    std::cerr << "error: "  <<  e.what()  <<  "\n";
}

特性:

  • 支持 Linux 和 Windows

  • 使用線程

  • 不依賴于其他非標準庫

另外一個示例:

#include <exec-stream.h>
...
exec_stream_t es;
try {    // run command to print network configuration, depending on the operating system
    #ifdef _WIN32
        es.start( "ipconfig", "/all" );
    #else
        es.start( "ifconfig", "-a" );
    #endif
    
    std::string s;
    while( std::getline( es.out(), s ).good() ) {        // do something withs
    }
}catch( std::exception const & e ) {
    std::cerr << "error: "  <<  e.what()  <<  "\n";
}

項目主頁:http://www.baiduhome.net/lib/view/home/1413111979375

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!