11

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??

7
  • 2
    What is "cv.h". And you don't (visibly) include the right header for system. You need <cstdlib> (then it is called std::system()) or <stdlib.h> (then it is system). Commented Jan 26, 2013 at 0:38
  • am developing the code. this is the starting phase. Thanks for your tipw :). Will correct it. Commented Jan 26, 2013 at 0:40
  • 1
    Do you mean you want to change the working directory of the shell from which you execute the C++ program? Commented Jan 26, 2013 at 0:47
  • yes. I was trying that. Now I thought am trying to execute commands in the new terminal opened, from the c++ program opened in the different shell! could you help me here? please see the edit. Commented Jan 26, 2013 at 0:50
  • superuser.com/questions/198015/… I think this is wat am looking for. But am not able to understand the answer. Could you pls verify if am on the right track @aschepler Commented Jan 26, 2013 at 0:55

3 Answers 3

11

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'");
Sign up to request clarification or add additional context in comments.

4 Comments

sorry bro. this is not wat am looking for. am sry if I wasn't clear in my question. pls see the edit.
The system function is equivalent to doing fork, exec, and waitpid, but easier. (For when you don't want to do any redirects, pipes, etc.)
should I use escape characters for the quote?? I feel something is slightly missing.
Yes, you can use single quota also
7

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.

6 Comments

he want to see next command executed after gnome-terminal being terminated, not right after.
Although chdir will change the CWD for your process, not its parent (the shell). Sounds like what the OP really wants is a shell script.
If you want to execute something WITHIN the gnome-terminal, you will need to do something different than the code you've just posted. There are multiple ways you could achieve something like what you are asking for - you could generate a shell-script file, and execute that, or you could open a pipe and send commands through that - or even open a pseudo-terminal and use the "master" side of that pseudoterminal to "write" to the actual terminal.
Even chdir() probably wouldn't have the desired effect, as it would only change the working directory for the C program, not for the parent shell. Shell function maybe?
Yes, chdir() did not work for me. I am a beginner and am not able to understand many of your comments. I am really sorry, but ould you please explain wat I can do??
|
0

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.

5 Comments

like what I gave in above comment/answer, huh?
yeah. actually, I was seeing another link,and only after I got back, saw ur answer, and thus verified this has to b the answer. But that link also said about including this exec bash command and other stuff, which was informatory, thus wanted to highlight here.
so my answer helped you and my answer is exactly what you are looking for, so you can accept the proper answer and close the question
Oh, sorry. Was looking for some other code, and I thot I had marked it as the answer.
@Vahid Farahmand, your answer doesn't end in exec bash, as his does. That can make a huge difference. exec bash keeps the terminal open when done. I have to use it in my answer here askubuntu.com/questions/315408/… (exec $SHELL works too) and here: askubuntu.com/questions/20330/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.