Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how should I call an external program from inside a C program

Tags:

c

I am currently working on moving some bash scripts over to C, and one of them call's an external python script, so for instance ./remoteclient01a.py "remotecommand player /something something"

./remoteclient01a.py "remotecommand player /something something"

and Ive been looking for a way to execute this command in C, but Im not really sure as to which I should use, being that System() seems to be the best one, but then a few pages say that it is a bad choice in some cases, so if someone could recommend a method to do this I would really appreciate it, thanks!

like image 765
lacrosse1991 Avatar asked Oct 24 '25 18:10

lacrosse1991


2 Answers

The only portable ways to do this in C are system and popen. The function popen allows you to read output from the command.

like image 75
cnicutar Avatar answered Oct 26 '25 08:10

cnicutar


  • For simple problems in unixish environments try popen().

From the man page


The popen() function opens a process by creating a pipe, forking and involking the
shell.
  • There are other means to call an external program in C. See the following tutorial explaining all the ways :

    Executing programs with C(Linux)

like image 30
pritam Avatar answered Oct 26 '25 08:10

pritam