C: Linux Command Executed By Popen() Function Not Showing Results


Answer :

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.


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable