LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to use c++ to use a command line program (https://www.linuxquestions.org/questions/programming-9/how-to-use-c-to-use-a-command-line-program-632239/)

jboss1995 04-01-2008 01:10 PM

How to use c++ to use a command line program
 
I was wondering how programs like k3b use command line programs like cdrecord, cdrdao, growisofs etc. Do they just use their library's or do they call the programs directly. If so, can you show me a short example. I'm trying to write a program that will use vmstat in a gui. Thanks for your help.

Maligree 04-01-2008 02:07 PM

Not 100% sure but I took a quick look at k3b's sources and it looks like it uses the console apps directly - since they're even described as "external applications" in the comments. You should take a look at the sources yourself - C++ isn't really my game and neither is Qt so the code isn't completely clear for me but you shouldn't have a problem with it. It's pretty well commented too.

Pretty sure other GUI apps do it the same way - it's easier and faster to use ready-to-go, external apps - the developers can focus on the usability of their program instead actually rewriting an already existing app (as it would happen if they decided to use only the libraries).

I may be wrong, I'm no developer. Yet. :)

ta0kira 04-02-2008 07:23 AM

You can use either system or execvp (et. al.)
ta0kira

matthewg42 04-02-2008 08:12 AM

For many programs, system, or one of the execs with forking is probably sufficient. If you have an interactive command line program, you might find libexpect useful. This allows you to spawn child processes, monitor their output, and interact with them. Expect is a language which extends TCL, but you can use libexpect from C and C++ programs.

jboss1995 04-05-2008 10:22 PM

Quote:

Originally Posted by matthewg42 (Post 3108231)
For many programs, system, or one of the execs with forking is probably sufficient. If you have an interactive command line program, you might find libexpect useful. This allows you to spawn child processes, monitor their output, and interact with them. Expect is a language which extends TCL, but you can use libexpect from C and C++ programs.

This is exactly what i was looking for. Thanks so much.


All times are GMT -5. The time now is 11:41 PM.