在c++中执行shell命令
我有一个关于在 C++ 中执行 shell 命令的问题。我正在 winforms 中构建一个应用程序,而不是 2008。我的应用程序有一个按钮,单击时应将二进制文件解码为 .csv 文件。我可以通过首先转到正确的目录(cd Test_Copy2)然后在命令提示符中执行命令(java -jar tool.jar -b x.fit x.csv)来解码文件。我尝试了很多不同的东西,但不幸的是没有一个起作用!
我尝试使用:
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)
任何人都可以为我提供一个如何做到这一点的示例吗?我不知道我哪里出了问题,大多数时候命令提示符打开但没有执行命令!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您确实想在 cmd.exe 实例中运行该 jar,则需要向 cmd.exe 添加正确的命令行开关之一,以便它按照您希望的方式工作:
例如,您的命令字符串应该是:
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:
For instance, your command string should be:
您可以使用
system()
函数来执行shell命令。例如:
system("DIR")
在 CMD shell 中执行 DIR 命令。开始时的默认目录是.exe
文件所在的目录。'system("PAUSE")` 执行 PAUSE 命令。
您想要执行的命令应作为常量字符串传递给函数。
编辑:
对于您的特定程序,语法(IMO)将是:
system("java -jar Tool.jar -b x.fit x.csv")
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")