Skip to content
Advertisement

Execute program from within a C program

How should I run another program from within my C program? I need to be able to write data into STDIN of the launched program (and maybe read from it’s STDOUT)

I am not sure if this is a standard C function. I need the solution that should work under Linux.

Advertisement

Answer

You want to use popen. It gives you a unidirectional pipe with which you can access stdin and stdout of the program.

popen is standard on modern unix and unix-like OS, of which Linux is one 🙂

Type

man popen

in a terminal to read more about it.

EDIT

Whether popen produces unidirectional or bidirectional pipes depends on the implementation. In Linux and OpenBSD, popen produces unidirectional pipes, which are read-only or write-only. On OS X, FreeBSD and NetBSD popen produces bidirectional pipes.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement