I just want to know which is the best way to execute an external command in C++ and how can I grab the output if there is any?
Edit: I Guess I had to tell that I'm a newbie here in this world, so I think I'm gonna need a working example. For example I want to execute a command like:
ls -la
how do I do that?
An example:
popen
definitely does the job that you're looking for, but it has a few drawbacks:If you want invoke a subprocess and provide input and capture output then you'll have to do something like this:
Of course, you can replace the
execlp
with any of the other exec functions as appropriate.Use the
popen
function.Example (not complete, production quality code, no error handling):
use system("ls -la") function