I’m writing a C program under Android/Linux that runs a system command. The command outputs some text to stdout, and I’m trying to capture the output into a string or character array.
For example:
system("ls");
would list the contents of the current directory to stdout, and I would like to be able to capture that data into a variable programmatically in C.
How do I do this?
Thanks.
You want to use
popen. It returns a stream, likefopen. However, you need to close the stream withpclose. This is becausepclosetakes care of cleaning up the resources associated with launching the child process.