I have finally worked out how to get stdin and stdout to pipe between the main app and a process created with CreateProcess (win32) or exec (linux). Now I am interested in harnessing the piping nature of an app. The app I am running can be piped into:
eg: cat file.txt | grep "a"
If I want to run “grep”, sending the contents of “file.txt” to it (which I have in a buffer in my c++ app), how do I do this? I assume I don’t just pump it down stdin, or am I wrong. Is that what I do?
Yes, that’s exactly what you do: read from
stdinand write tostdout.One of the strokes of genius behind linux is the simplicity of redirecting input and output almost effortlessly, as long as your apps obey some very simple, basic rules. For example: send data to
stdoutand errors or informational messages tostderr. That makes it easy for a user to keep track of status, and you can still use your app to send data to a pipe.You can also redirect data (from
stdout) and messages (fromstderr) independently:Redirection may be a bit confusing at first, but you’ll find the time spent learning will be well worth it!