system()的返回值不是执行程序的返回值
我想执行一个可执行文件,其main()使用system()返回2.
这就是我做的 #include <stdio.h> #include <string.h> int main(int argc,char *agrv[]) { char command[7]; strcpy(command,"./test1"); printf("The return value: %dn",system(command)); return 0; } 和test1是 #include <stdio.h> int main(void) { printf("test1 has been executed and its return value is 2n"); return 2; } 这就是我得到的 test1 has been executed and its return value is 2 The return value: 512 我的问题是为什么我得到512. 解决方法
系统的返回值实际上是POSIX下waitpid()的返回值.
status实际上嵌入了很多信息: 从系统(3)手册页:
解 #include <stdio.h> #include <string.h> #include <limits.h> int main(int argc,char *argv[]) { int status; char command[PATH_MAX]; /* PATH_MAX is defined in sys/syslimits.h,included by limits.h */ strcpy(command,"./test1"); status = system(command); if ( WIFEXITED(status) ) { printf("The return value: %dn",WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("The program exited because of signal (signal no:%d)n",WTERMSIG(status)); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |