Try to execute command line codes from c++ linux - c++

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.

Related

VSCode Extension Run Code can't run C++ in terminal

I am new in C++. I like VSCode very much. I want to run my C++ code in VSCode and so I am using MinGW and Run Code Extension of VSCode.
MinGW setup worked and I could also run my C++ code in the output terminal in VSCode, but when I enabled Code Runner in the settings to run my code in the terminal (so that I can take inputs), I am getting this error:
bash: cd: d:\CodeForces" && g++ inSearchOfAnEasyProblem.cpp -o inSearchOfAnEasyProblem && d:CodeForces"inSearchOfAnEasyProblem: No such file or directory
My Folder structure is: d://CodeForces//inSearchOfAnEasyProblem.cpp
I am trying to run this file: inSearchOfAnEasyProblem.cpp
I am using the Bash terminal. I tried to change the terminal to cmd, but when I click the run button of code runner, it always runs in a bash terminal.
enter image description here
Can anyone please help me? I would have been grateful.

C++ executable, sh 1:not found

I created a c++ programm that works with ros. The first step would be to open a roscore in a terminal and move on from there. I do so with system("roscore &");
I compiled my file and can run it just fine with ./file.
However, I want to be able to run it as an application (double click). I created a .desktop file and the program shows up in my application list. When i start it though, all I get is a terminal that opens with the message
sh: 1: roscore: not found
etc.
The same applies for the roslaunch commands. I also fork and exec a roslaunch command, which does not work as well.
I tried system("ls"); which worked. All cout messages work as well.
Any idea what is wrong here?
roscore executable is not located in std paths (/bin:/usr/bin:). Use the absolute path - system("/path/to/roscore &")

how to make terminal prompt messages?

I have a image processing project in C++ using opencv. The program runs correctly and I get the desired output. However, I have some messages that I print out using the cout command. When I run the program using the terminal (./myprogram) the messages are displayed correctly. When I double click the executable file I get only the output (in my case a new video is created) But I do get the messages. How do I get the program to automatically prompt the messages when it is not run from the terminal.
PS: I use ubuntu 14.04
Create a script like this, lets call it run.sh:
#!/bin/sh
cd work_dir
./myProgram
read -r -p "Press any key..." key
Then do:
xterm -e run.sh
and make your desktop shortcut run this command instead of the program directly.

Stop error output in C++ when making new folder

I am using C++ on Ubuntu. I have been using the command:
system("mkdir new_folder");
to make a new folder called new_folder. However, if that folder already exists, C++ outputs an error message (and continues to run afterwards).
Is there a way to stop the error message from printing out?
For this particular command use mkdir -p new_folder.
Generally, you want to fork your process and on one of the branches redirect stdout and stderr to /dev/null or similar then do exec to replace the process with the new one.

What needs to be done to get a distributable program from Eclipse?

I’ve produced a C++ program in Eclipse running on Redhat, which compiles and runs fine through Eclipse.
I thought that to run it separately to Eclipse you use the build artifact which is in the directory set via the project’s properties.
However this executable doesn’t run (I know it’s an executable as I’ve set it to be an executable via the project’s properties and it shows up as such via the ls command and the file explorer).
When attempting to run it using the executable’s name, I get the error:
bash: <filename>: command not found
When attempting to run it as a bash file:
<filename>: <filename>: cannot execute binary file
And when running it with "./" before the file name, nothing happens. Nothing new appears in the running processes and the terminal just goes to the next line as though I’d just pressed enter with no command.
Any help?
You've more or less figure out the first error yourself. when you just run <filename> , it is not in your PATH environment variable, so you get "command not found". You have to give a full or relative path when to the program in order to run it, even if you're in the same directory as the program - you run it with ./<filename>
When you do run your program, it appears to just exit as soon as you start it - we can't help much with that without knowing what the program does or see some code.
You can do some debugging, e.g. after the program just exits run echo $? to see if it exited with a particular exit value, or run your program using the strace tool to see what it does (or do it the usual way, insert printf debugging, or debug it with gdb)