Reputation: 1834
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?
Upvotes: 0
Views: 338
Reputation: 48330
Yes, that's exactly what you do: read from stdin
and write to stdout
.
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 stdout
and errors or informational messages to stderr
. 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 (from stderr
) independently:
myapp | tail -n 5 > myapp.data # Save the last 5 lines, display msgs
myapp 2> myapp.err | sort # Sort the output, send msgs to a file
myapp 2> /dev/null # Throw msgs away, display output
myapp > myapp.out 2>&1 # Send all output (incl. msgs) to a file
Redirection may be a bit confusing at first, but you'll find the time spent learning will be well worth it!
Upvotes: 1