为何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$
有什么办法在我的程序里面得到我运行的命令的返回值啊?
分有的是。