尝试从C ++ Linux执行命令行代码 [英] 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");
}
gnome-terminal命令执行得很好。我关闭终端后,当期望CD执行时,却没有发生。您能帮我指出原因吗?谢谢。我期待该函数使cmd进入主目录
,但事实并非如此。在linux中工作
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
即使删除gnome,我也尝试过。简单的CD无法正常工作。我在做rong>吗?
I tried it even by removing gnome. simple cd is not working. am I doing something rong>?
如果我尝试ls,它似乎工作正常!
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??
推荐答案
如果您要运行程序并等待其完成,可以请您告诉我如何实现此目标?在执行下一行之前,请查看以下页面和示例代码: http://www.thegeekstuff.com/2012/03/c-process-control-functions/
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/
但是如果您要运行gnome-terminal并在新创建的窗口中执行命令,请执行以下操作:
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'");
这篇关于尝试从C ++ Linux执行命令行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!