在C程序中执行一个Linux命令
问题描述:
我试图用系统的系统调用C程序执行Linux命令,但不希望它倾倒在终端上的输出或错误日志。我该怎么办?是否有任何其他方式做到这一点?
I am trying to execute a Linux command in c program using system system call, but the don't want it to dump the output or error logs on the terminal. What should I do? Is there any other way to do this?
答
由于系统()调用使用shell执行命令,可以输出和错误重定向到/ dev / null的,例如
As the system() call uses a shell to execute the command, you can redirect stdout and stderr to /dev/null, e.g.
system("ls -lh >/dev/null 2>&1");