我尝试在c程序中使用系统系统调用执行Linux命令,但不希望它在终端上转储输出或错误日志。我该怎么办?有没有其他方法可以做到这一点?
发布于 2011-01-21 18:09:53
由于system()调用使用shell来执行命令,因此可以将stdout和stderr重定向到/dev/null,例如
system("ls -lh >/dev/null 2>&1");发布于 2014-07-26 06:14:56
popen是另一种你可以这样做的方式:
void get_popen() {
FILE *pf;
char command[20];
char data[512];
// Execute a process listing
sprintf(command, "ps aux wwwf");
// Setup our pipe for reading and execute our command.
pf = popen(command,"r");
// Error handling
// Get the data from the process execution
fgets(data, 512 , pf);
// the data is now in 'data'
if (pclose(pf) != 0)
fprintf(stderr," Error: Failed to close command stream \n");
return;
}发布于 2011-01-21 18:10:14
向您展示代码。
请尝试以下示例:
系统(“ls”);
https://stackoverflow.com/questions/4757512
复制相似问题