在c ++中执行shell命令 [英] Execute shell command in c++
问题描述
我有一个关于在c ++中执行shell命令的问题。我在winforms,2008年建立一个应用程序。我的应用程序有一个按钮,当点击应该解码一个二进制文件为.CSV文件。我可以解码文件,首先转到正确的目录(cd Test_Copy2),然后在命令提示符(java -jar tool.jar -b x.fit x.csv)中执行命令。我尝试了很多不同的东西,但不幸的是没有工作!
I have a question regarding executing shell commands in c++. I'm building an application in winforms, vs 2008. My application has a button, when clicked should decode a binary file to a .csv file. I can decode files by first going to the right directory (cd Test_Copy2) and then execute a command in the command prompt (java -jar tool.jar -b x.fit x.csv). I tried a lot of different stuff but unfortunately got none to work!
我尝试使用:
system, _popen, ShellExecute(NULL, L"open", L"C:\\WINDOWS\\system32\\cmd.exe ", L"java -jar Tool.jar -b x.fit x.csv", L"C:\\Test_Copy2", SW_SHOWNORMAL)
任何人都可以给我一个例子,如何做到这一点?我不知道我在哪里出错,大多数时候命令提示符打开,但没有执行命令!
Can anyone please provide me with an example on how to do that? I dont know where I'm going wrong, most of the time the command prompt opens but no command is executed!
推荐答案
可以使用 system()
函数来执行shell命令。
例如:
system(DIR)
在CMD shell中执行DIR命令。开始时的默认目录是您 .exe
文件所在的目录。
'system(PAUSE)`执行PAUSE命令。
要执行的命令应作为常量字符串传递给函数。
You can use the system()
function to execute shell commands.
For example:
system("DIR")
executes the DIR command in the CMD shell. The default directory at the start is the directory you're .exe
file is located.
'system("PAUSE")` executes the PAUSE command.
The command/s you wannt to execute should be passed as a constant string to the function.
对于paritcular程序,语法(IMO)将是:
For you paritcular program the syntax (IMO) would be:
system(java -jar Tool.jar -b x .fit x.csv)
这篇关于在c ++中执行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!