如何从C ++程序执行命令行命令 [英] How can I execute a command line command from a C++ program
问题描述
如何在c ++中执行命令行 asterisk -rx reload?请帮忙。我需要一个例子。我正在ubuntu服务器上工作,我想从用户(在Web服务内部)执行此命令行。
How can I execute the command line "asterisk -rx "reload"" in c++? Please help. I need an example. I am working on ubuntu server and I want to execute this command line from a user (inside a webservice).
需要帮助
赞赏
Need help Appreciate
推荐答案
听起来像 system()
函数:
Sounds like a trivial use-case for the system()
function:
system("asterisk -rx reload");
如果您需要对子进程进行非常细粒度的控制,则有更好的方法,但这很简单
If you need very fine-grained control of the child process there are better ways, but this is simple to get going.
此调用启动一个shell(例如bash)来运行命令,这就是为什么我删除了 reload
;它们对于一个单词来说毫无意义,反正会被外壳删除,而且无论如何都不会被启动程序看到。
This call starts a shell (such as bash) to run the command, which is why I removed the quotes around reload
; they're pointless for a single word and will be removed by the shell and never seen by the started program, anyway.
这篇关于如何从C ++程序执行命令行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!