c++ - how to read command output line by line in gcc in windows just as with the standard input? -
this tried:
#include <iostream> #include <string> int main(int argc, char const *argv[]) { using namespace std; (string cin_line; getline(cin, cin_line);) { cout << cin_line << endl; } file* pipe = popen("app.exe", "r"); (string result_line; getline(pipe, result_line);) { cout << result_line << endl; } pclose(pipe); return 0; }
it doesn't compile, result is:
no matching function call 'getline(file*&, std::__cxx11::string&)'
second example i've found here: https://stackoverflow.com/a/10702464/393087 seems mingw doesn't have pstream included: fatal error: pstream.h: no such file or directory
- edit: ok know, missed not gcc library, named separate download: http://pstreams.sourceforge.net/
i know how using buffer , whole output on single line (like here: https://stackoverflow.com/a/478960/393087 ) explode line \n
, array, point here must provide output input comes in.
also tried example here: https://stackoverflow.com/a/313382/393087 - i've added main
function that:
#include <cstdio> #include <iostream> #include <string> int main(int argc, char const *argv[]) { using namespace std; file * fp ; if((fp= popen("/bin/df","r")) == null) { // error processing , exit } ifstream ins(fileno(fp)); // ifstream ctor using file descriptor string s; while (! ins.eof()){ getline(ins,s); // } return 0; }
this doesn't compile:
error: variable 'std::ifstream ins' has initializer incomplete type
ifstream ins(fileno(fp)); // ifstream ctor using file descriptor
you can't this:
file* pipe = popen("app.exe", "r"); (string result_line; getline(pipe, result_line);) { cout << result_line << endl; } pclose(pipe);
you need this:
#include <boost/noncopyable.hpp> #include <boost/iostreams/stream.hpp> #include <boost/iostreams/device/file_descriptor.hpp> file* pipe = popen("app.exe", "r"); boost::iostreams::file_descriptor_source source(fileno(pipe), boost::iostreams::never_close_handle); boost::iostreams::stream<boost::iostreams::file_descriptor_source> stream(source, 0x1000, 0x1000); string result_line; while (getline(stream, result_line)) { cout << result_line << endl; }
:)
Comments
Post a Comment