C: Linux command executed by popen() function not showing results

Since the output is going to stderr you need to redirect stderr like so:

FILE* file = popen("ntpdate 2>&1", "r");

this will redirect stderr to stdout and so you will see output from both. Second issue fscanf will stop at the first space so you can replace with fgets:

fgets(buffer, 100, file);

As Shafik Yaghmour correctly diagnosed, the output you see from ntpdate is written (correctly) to its standard error, which is the same as your programs standard error.

To get the error messages sent down the pipe, use:

FILE *file = popen("ntpdate 2>&1", "r");

That sends the standard error output from ntpdate to the standard output of the command, which is the pipe you're reading from.

Of course, it looks like using ntpdate isn't going to work well until you've configured something.

Tags:

Linux

C

Popen