当前位置: 技术问答>linux和unix
用system函数,如何知道该函数执行成功了?
来源: 互联网 发布时间:2015-08-25
本文导语: 用system函数,如何知道该函数执行成功了? | 补充一点,要用system 获得返回值, 最好在调用的main()函数中加上返回值, 比如:下面的程序名称为test.c int main() { if () { ......
用system函数,如何知道该函数执行成功了?
|
补充一点,要用system 获得返回值,
最好在调用的main()函数中加上返回值,
比如:下面的程序名称为test.c
int main()
{
if ()
{
...
exit(-1);
}
else
{
...
exit(1);
}
}
这样在system("test")时 可以取得test的返回值
int flag;
flag =system("test");
if (flag==1) printf("success!n");
else if (flag==-1) printr("fail!n");
最好在调用的main()函数中加上返回值,
比如:下面的程序名称为test.c
int main()
{
if ()
{
...
exit(-1);
}
else
{
...
exit(1);
}
}
这样在system("test")时 可以取得test的返回值
int flag;
flag =system("test");
if (flag==1) printf("success!n");
else if (flag==-1) printr("fail!n");
|
非也,system的返回值与main的返回值是两回事。只能得到成功或失败的信息
|
如果system返回不是-1的话表明执行成功
rc = system("...");
WEXITSTATUS(rc)可以取被执行程序的返回值,该值的有效范围为0-127,就是main函数return的值或是exit的值
rc = system("...");
WEXITSTATUS(rc)可以取被执行程序的返回值,该值的有效范围为0-127,就是main函数return的值或是exit的值
|
int system( const char *command );
Return Value:If command is NULL and the command interpreter is found, the function returns a nonzero value. If the command interpreter is not found, it returns 0 and sets errno to ENOENT. If command is not NULL, system returns the value that is returned by the command interpreter. It returns the value 0 only if the command interpreter returns the value 0. A return value of – 1 indicates an error, and errno is set to one of the following values:
E2BIG
Argument list (which is system-dependent) is too big.
ENOENT
Command interpreter cannot be found.
ENOEXEC
Command-interpreter file has invalid format and is not executable.
ENOMEM
Not enough memory is available to execute command; or available memory has been corrupted; or invalid block exists, indicating that process making call was not allocated properly.
|
If system return 0 , then success
|
我做的代码里面,这样写的
int rc=0;
rc=system("testname")
if(rc==0)
cout
int rc=0;
rc=system("testname")
if(rc==0)
cout