C中system()的返回值 [英] return value of system() in C

查看:19
本文介绍了C中system()的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C 中使用 system() 命令来执行 sc query mysqlnet start mysql 等命令.

I am using the system() command in C to execute commands like sc query mysql or net start mysql.

如果参数为空指针,则cmd处理器正常返回1,否则返回0.命令执行成功返回0.

If the parameter is null pointer then it returns 1 if the cmd processor is OK, otherwise it returns 0. On successful command execution it returns 0.

我的问题是:我可以得到它的返回值列表吗?比如如果命令无效会返回什么,或者执行不成功的返回值是什么?我想根据 system() 的返回值做不同的事情.

My question is: Can I get a list of its return values? Like what it will return if the command is invalid or what the return value on unsuccessful execution will be? I want to do different things depending on the return value of system().

推荐答案

作为 docsstate system() 如果为要执行的命令创建新进程失败,则返回-1,否则返回已执行命令的退出代码.这与在 shell 中执行相同命令后,您可以使用 unix 中的 echo $? 或 windows 中的 echo %ERRORLEVEL% 检索到的值相同.因此,如果您想处理返回值,则必须查看执行的命令返回什么.

As the docs state system() return -1 if creating the new process for the command to be executed fails, otherwise it returns the exit code of the command executed. this is the same value you can retrieve using echo $? in unix or echo %ERRORLEVEL% in windows after executing the same command in a shell. So if you want to handle the return values you have to look at what the commands executed return.

这篇关于C中system()的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆