system()

When i try to execute some shell command from my C program using system(), how can I capture the
error that occurs due to the shell command?

The system() command's return values will say if the system() was successfull and not if the shell command is successfull.

When I tested my program, I saw that the shell command didnt execute and the error was thrown in standard output,
but the system() was successful.

Got any idea how I can capture the failure of the shell command?

Deepa

You can output(redirect) the error to some log file and see whether the error is captured there.

You can check the exit status of system command with the help of the following macros :

#include <sys/types.h>
#include <sys/wait.h>

int ret = WIFEXITED ( system ( "command" ) ) ;
or
int ret = WEXITSTATUS ( system ( "command" ) ) ;

Thanks Nisha & Prasad!

I also figured out another way of executing shell commands from C program where we can check the exit status. ...... using popen().

Deepa