首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

为何int ret=system("cmd") 不能返回正确的返回值。解决方法

2012-02-25 
为何int retsystem(cmd) 不能返回正确的返回值。在linux下面,在一般的shell下,比如我运行一个命令。machi

为何int ret=system("cmd") 不能返回正确的返回值。
在linux   下面,   在一般的shell下,比如我运行一个命令。
machine$   ls   /tm
/bin/ls:   /tm:   No   such   file   or   directory
machine$   echo   $?
1
machine$


如果我把这个命令用system( "ls   /tm ");   为何我不能得到返回值1啊?


iclx012$   more   dun.c
#include   <unistd.h>
#include   <iostream>

using   namespace   std;
int
main(   int   argc,   char   **argv   )
{
      int   ret;

      ret   =   system( "ls   /tm ");

                cout < < "ret= " < <ret < <endl;
      exit(   ret   );
}

iclx012$   g++   dun.c
iclx012$   ./a.out  
ls:   /tm:   No   such   file   or   directory
ret=256
iclx012$   echo   $?
0
iclx012$

[解决办法]
cout < < "ret= " < < WEXITSTATUS(ret) < < endl;

热点排行