Capturing stdout from a system() command optimally

c, c++, stdout, system

Solution

From the popen manual:

#include <stdio.h>

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

int pclose(FILE *stream);

Problem

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++?

Original source

Related problems