Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 603
Lakshmi Narayanan Avatar asked Jan 26 '13 00:01

Lakshmi Narayanan


1 Answers

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'");
like image 115
Vahid Farahmand Avatar answered Oct 02 '22 14:10

Vahid Farahmand