get command output in pipe, C for Linux

后端 未结 4 1114
一向
一向 2020-12-04 00:19

I need to run a Linux CLI command and get its stdout output from C.

I can use pipe() to create a pipe, then fork/exec, redirecting child\'s stdout descriptor into th

4条回答
  •  长情又很酷
    2020-12-04 00:48

    Use popen() and pclose().


    popen() does not actually wait, of course, but reads on the pipe will block until there is data available.

    pclose() waits, but calling it prematurely could cut off some output from the forked process. You'll want to determine from the stream when the child is done...


    Possibly already discussed at How can I run an external program from C and parse its output?

提交回复
热议问题