看到我使用一个系统调用在我的C代码如何获得命令的系统运行()状态
#include <sys/stat.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
int a = system("./test12.out"); //here if i am giving any wrong commad
system("echo $?")
printf("system return is %d",a);
}
心不是我的当前文件夹中的任何文件test12.out。现在输出是
sh: ./test12.out: No such file or directory
0
system return is 32512
这里我的shell命令得到失败,但我怎么能知道在我的c代码?
编辑:
所以我可以去这样
int
main(int argc, char *argv[])
{
int a = system("dftg");
if(a == -1)
printf("some error is occured in that shell command");
else if (WEXITSTATUS(a) == 127)
printf("That shell command is not found");
else
printf("system call return succesfull with %d",WEXITSTATUS(a));
}
+1,删除了我自己的答案。一定要单独检查“-1”。 –
现在看我的编辑...是否正确? –