Programming c++ to run commands in a particualr directory
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Programming c++ to run commands in a particualr directory
Okay newbie to code I may be but I´m writing an IDE like program at the moment.
The problem is that I need a way to get the program to run a command that the user has
specified at the push of a button, and as you can imagine make won´t do so well when it is running in /usr/bin and the targets are in /home/$USER/generic_program.
Can somebody explain the code needed to make sure that the command is run in the right directory?
PS: if there is a KDE realted method that would be optimal.
Perhaps... I´ll have to test it on my own computer (I post of a Damn Small box that doesn´t even have man!
Also, what I forgot is even a bigger matter is how to make these commands run inside of an xterm or konsole (as you can probably tell I´ve bitten of more than I can chew here).
It would be better if you'd specify what you want a little more precise.
What I gathered so far is that you want to call a command from inside a C or C++ program, and this command should be called from a particular directory and in a terminal. This is relatively easy. You get to the proper directory by doing chdir, you execute a command by doing popen() or exec and you specify that your command should run in a terminal by calling this terminal with the corresponding option.
This is relatively easy. You get to the proper directory by doing chdir, you execute a command by doing popen() or exec and you specify that your command should run in a terminal by calling this terminal with the corresponding option.
The canonical technique is to do child process setup (such as chdir) after the fork but before the exec. That way, you don’t mess up your own process’ state (i.e., the directory is changed in the child but left alone in the parent).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.