【发布时间】:2012-02-15 00:50:32
【问题描述】:
我在我的 c 代码中使用了一个系统调用
#include <sys/stat.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
int a = system("./test12.out"); //here if i give any wrong command
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 has 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));
}
【问题讨论】: