LibexecstreamC++ 库
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"; }
评论