Execute shell command in c++ - c++

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!

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

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")

Related

How to run iostream system command in the same thread?

I'm trying to execute a command using std::system from Unreal Engine C++
FString command = FString("start /B cmd /k python \"D:/app.py\"");
std::string comm(TCHAR_TO_UTF8(*command));
std::system(comm.c_str());
The command itself is working as expected, however, I need it to execute on the current thread, or alternatively check if it's finished before continuing because the next operations depend on the completion of this command
Is there a way to do it? or maybe I should use another command?
Thanks, Eden
The std::system function will not return until the command you execute have finished running.
Also on Windows (which you seem to be running) then system will invoke the command interpreter (cmd) for execution of the command, which means the command you want to execute must be in the command interpreters PATH (or be an internal command of the command interpreter).
If python is in the PATH, then you could run the python command directly, without using start or cmd (especially since then you would have two instances of cmd running), and the system function would block and not return until the python command finished running:
FString command = FString("python \"D:/VRoscopy_repo/VRoscopy/conversion/invesalius3-master/app.py\" --no-gui -i \"D:\VRoscopy_repo\DICOM\Human\MedDream\Body\" -t 200,3033 -e \"D:\VRoscopy_repo\DICOM\Human\MedDream\Body/VRoscopy27777BED4B650CE6AFE884B365C56BCC.stl\"");

QProcess can launch programs but not python (command line)

The following code is failing to launch the python command line.
QProcess *myProcess = new QProcess(this);
myProcess->start("\"C:\\Program Files\\Python27\\python.exe\"");
If I replace python27 with (for example)
myProcess->start("\"C:\\Program Files\\Notepad++\\notepad++.exe\"")
notepad opens. Why is my program able to launch notepad but not Python Command Line?
I tried using startDetached() as suggested here but that didn't make a difference.
QProcess::Error() gives me error 5: unknown error.
If you just want to use the 'python console' you must use cmd.exe application from windows
You must have python in PATH so the windows console will know where to take it from.
So, you can try: QProcess::startDetached("cmd", "python")..see more specific syntax details here
It seems I've misunderstood what happens when you launch a command line. I was expecting the python command line or command prompt window to open.
It turns out that if I just pass my commands as arguments start() like so:
myProcess->start("cmd.exe /C python C:\\Users\\SP4\\Desktop\\helloworld.py");
Command prompt runs my python script and I get the output ("Hello World") using:
QString output = myProcess->readAllStandardOutput();
All this happens in the background and you can't actually see a command line window open and print out "Hello, World".
Please correct me if I've misunderstood something.

Console prompt window appear on system("start dir") but not on system("start ipconfig")

I try to create a simple UI which runs a command prompt in the background (but the windows console must not disappear) while clicking on each button, resp.
But before, I try something like system("start dir"); to see if the button works.
Here is the problem: when I click on the left button the windows console appear and don't exit unit I close it. But this only work with system("start dir");. If I change dir to ipconfig (or another call-function) the windows console will appear for a second and the exit. I tried something like system("PAUSE"); or getch(); etc, but it doesn't work.
Why does this command work with dir but not with another command?
There is a fundamental difference between DIR and IPCONFIG, the DIR command is built into the command processor (aka shell), IPCONFIG is a separate program stored in c:\windows\system32.
When you type START /? at the command line then you can see why it treats them differently:
If it is an internal cmd command or a batch file then
the command processor is run with the /K switch to cmd.exe.
This means that the window will remain after the command
has been run.
If it is not an internal cmd command or batch file then
it is a program and will run as either a windowed application
or a console application.
The alternative is to ask the command processor to execute the command and exit afterwards. You do with the /c option:
system("cmd.exe /c dir");
Or simpler yet, since system() automatically passes off the job to the command processor:
system("dir");
Just stop using start :)

How to launch terminal in a specific folder?

I've tried both launching /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal as a process and using std::system to launch it from sh script (open -a /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal) with different arguments (detsdir as argument, cd destdir etc.). Nothing works. It either opens in ~/, or it says "destdir is a directory` and logs off.
I've also tried launching it as a process with destdir set as working directory (using QProcess::startDetached). No luck either, it opens in ~/.
You can do it like this, with osascript:
osascript -e "tell application \"Terminal\" to do script \"cd ~/Desktop\""
This will create a new terminal window with working directory ~/Desktop.
QProcess has method setWorkingDirectory()

Try to execute command line codes from c++ linux

I tried the following code, to communicate with the command line from c++ code.
#include<iostream>
#include<cv.h>
int main()
{
system("gnome-terminal");
system("cd");
}
The gnome-terminal command is executing fine. After I close the terminal, when am expecting the cd to execute, however, is not happening. Could you please help me and point out the reason? Thanks. I was expecting the function to make the cmd go down to the home directory
, but it did not. am working in linux
I tried it even by removing gnome. simple cd is not working. am I doing something rong>?
If I try ls, it seems to be working fine!
My main aim is to open a new terminal, and execute commands on that new terminal through the present program that opened the new terminal. Could you please tell me how I can achieve this??
If you want to run a program and wait for it to finish before executing next line, take a look at this page and example code here: http://www.thegeekstuff.com/2012/03/c-process-control-functions/
But if you want to run gnome-terminal and execute a command in newly created window, do this:
system("gnome-terminal -x sh -c 'cd /tmp ; ls -la'");
The system function creates a shell child process to execute the specified command.
cd is a shell command which changes the current working directory of that shell process only.
So the child's cd probably works fine, but it has no effect on your C++ program, which is a different process.
Instead, you probably want to look at the Linux system call chdir.
Thanks for your help!! This command worked perfectly fine from this link
https://superuser.com/questions/198015/open-gnome-terminal-programmatically-and-execute-commands-after-bashrc-was-execu
gnome-terminal -x sh -c 'command1; command2; exec bash'
and I entered the respective commands in the new window. But to change the working directory in the shell am working o, I haven't still figured that out.