Execute shell command in c++

放肆的年华 提交于 2019-12-10 12:24:53

问题


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!

I tried using:

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!


回答1:


If you really want to run the jar in a cmd.exe instance, you need to add one of the correct command line switches to cmd.exe for it to work the way you want it to:

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains

For instance, your command string should be:

C:\\WINDOWS\\system32\\cmd.exe /c java -jar Tool.jar -b x.fit x.csv



回答2:


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.

Edit:

For you paritcular program the syntax (IMO) would be:

system("java -jar Tool.jar -b x.fit x.csv")



来源:https://stackoverflow.com/questions/7232304/execute-shell-command-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!