I am having trouble with finding out how to use popen() to grab stdout from child programs in Linux to a main C++ program. I was looking around and found this snippet of code that does what I want it to do. But I can't understand how this stuff works. I know the basics of c++ programming (I've been doing it for several months now) but i'm stumped so can someone help me with an explanation of this?
Thank you in advance.
#include <vector>
#include <string>
#include <stdio.h>
#include <iostream>
using namespace std;
void my_popen( const string& cmd ,vector<string>& out ){
FILE* fp;
const int sizebuf=1234;
char buff[sizebuf];
out = vector<string> ();
if ((fp = popen (cmd.c_str (), "r"))== NULL){
}
string cur_string = "";
while (fgets(buff, sizeof (buff), fp)) {
cur_string += buff;
}
out.push_back (cur_string.substr (0, cur_string.size() -1));
pclose(fp);
}
int main () {
vector<string> output;
my_popen("which driftnet", output);
for (vector<string>::iterator itr = output.begin();
itr != output.end();
++itr) {
cout << *itr << endl;
}
return 0;
}