mercredi 1 avril 2015

how to read command output line by line in gcc in windows just as with the standard input?

This is what I tried:



#include <iostream>
#include <string>

int main(int argc, char const *argv[]) {
using namespace std;
for (string cin_line; getline(cin, cin_line);) {
cout << cin_line << endl;
}
FILE* pipe = popen("app.exe", "r");
for (string result_line; getline(pipe, result_line);) {
cout << result_line << endl;
}
pclose(pipe);
return 0;
}


It doesn't compile, the result is:

no matching function for call to 'getline(FILE*&, std::__cxx11::string&)'


Second example I've found here: http://ift.tt/1C7QKcz But it seems mingw doesn't have pstream included: fatal error: pstream.h: No such file or directory


I know how to do it using buffer and get whole output on single line (like here: http://ift.tt/1OZ1l3q ) then explode the line by \n and get my array, but the point here is that I must provide the output as soon as the input comes in.


Also I tried example from here: http://ift.tt/1C7QKsQ - I've added main function to 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 and exit
}
ifstream ins(fileno(fp)); // ifstream ctor using a file descriptor

string s;
while (! ins.eof()){
getline(ins,s);
// do something
}
return 0;
}


This also doesn't compile:

error: variable 'std::ifstream ins' has initializer but incomplete type ifstream ins(fileno(fp)); // ifstream ctor using a file descriptor


Aucun commentaire:

Enregistrer un commentaire