0

When using the system() function in C code, it appears that in many cases that it doesn't provide errno results but exit codes or simply writes to stdout and stderr.

My question is how can I get the results from system() before they go to stdout or stderr? Can the output be piped to a C function. I read this MS document provided in this question but am not sure.

I think stdout and stderr can be redirected to a file and read from there but is there a better way to capture the output?

Also, is the text file layout consistent between OS versions, and where can that information be found?

For a simple example, could the results of system( "dir > dirList" ) be programmatically captured in C to build a custom listing? I know that there is are readdir() and stat() in the dirent.h header to get this information but it's just an example to illustrate.

Thank you.

dxiv
  • 16,984
  • 2
  • 27
  • 49
Gary
  • 2,393
  • 12
  • 31
  • What you want is `popen()`, but I don't know to what extent Windows supports it. – Nate Eldredge Feb 27 '21 at 04:44
  • https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/popen-wpopen?view=msvc-160 – Retired Ninja Feb 27 '21 at 04:45
  • 1
    For Windows specifically, see also [Creating a Child Process with Redirected Input and Output](https://learn.microsoft.com/en-us/windows/win32/procthread/creating-a-child-process-with-redirected-input-and-output) and this [previous question](https://stackoverflow.com/questions/35969730/how-to-read-output-from-cmd-exe-using-createprocess-and-createpipe). – dxiv Feb 27 '21 at 04:46

0 Answers0