Capturing stdout from a system() command optimally

SinisterDex picture SinisterDex · Sep 24, 2008 · Viewed 81.5k times · Source

I'm trying to start an external application through system() - for example, system("ls"). I would like to capture its output as it happens so I can send it to another function for further processing. What's the best way to do that in C/C++?

Answer

jkramer picture jkramer · Sep 24, 2008

From the popen manual:

#include <stdio.h>

FILE *popen(const char *command, const char *type);

int pclose(FILE *stream);