LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   running linux ccommands in a C program (https://www.linuxquestions.org/questions/programming-9/running-linux-ccommands-in-a-c-program-369005/)

DJOtaku 10-02-2005 08:54 AM

running linux ccommands in a C program
 
for the sake of arguement, let's say I was writing a C or C++ gui program. In this program I wish to have the user supply arguements which I would then use to run a command.

eg A front end to lame

So I want them to select a bitrate and filename then I want to execute the command

lame -b bitrate filename.mp3

This is just an example, so if I have the lame syntax wrong, don't worry. I was just curious what I had to do to execute linux commands from within a C/C++ program.

Thanks,

nixcraft 10-02-2005 09:16 AM

Use system() or exec() function.

Code:

main(){
....
system("ls /etc");
...

Read man page of exec and system.

Valhalla 10-02-2005 10:33 AM

you might also want to looking into the popen. It opens a stream to a command, meaning your program will receive the output from the command instead of it being sent straight to stdout.

DJOtaku 10-02-2005 06:41 PM

thanks a lot! Esp Nix, that was very helpful

cppkid 10-03-2005 05:13 AM

I think if you will use System(), it directly it will shift the control to that command, so if you want to create a child process that will execute your command and your original application will continue normally you can use fork() and exec(). see "man fork" for details.


All times are GMT -5. The time now is 12:39 PM.