piping standard input to a program?

Rick Stevens rstevens at vitalstream.com
Thu Jan 12 01:40:04 UTC 2006


On Wed, 2006-01-11 at 15:24 -0800, mike wrote:
> so i have a program that takes standard input as input.  it's run from
> a shell like so:
>  
> echo -e "blah blah blah" | <program> <options> 
>  
> my question is how would i do this in like C/C++ code?  how do i in
> c/c++ take standard input and pipe it to this program as input?

Well, here's a C code fragment:

	int main(int argc, char **argv) {

	    char	buff[256];
	    int		x;

	    x = fread(stdin, buff, 255);

By default, the C and C++ startup code has three buffered file
descriptors opened:

	Name in C	Name in C++
	stdin		cin
	stdout		cout
	stderr		cerr

These also correspond to the unbuffered file descriptors 0, 1 and 2,
respectively (0 is stdin/cin, 1 is stdout/cout, 2 is stderr/cerr).

This is all very well described in the various C and C++ primers.

These are, BTW, the same as the shell's "0", "1" and "2" descriptors.
You know, to send stdout and stderr to /dev/null, you'd do something
like:

	echo "Garbage" >/dev/null 2>&1

The ">/dev/null" sends stdout to /dev/null, the "2>&1" means "send
stderr to the same spot as stdout".
----------------------------------------------------------------------
- Rick Stevens, Senior Systems Engineer     rstevens at vitalstream.com -
- VitalStream, Inc.                       http://www.vitalstream.com -
-                                                                    -
-              Death is nature's way of dropping carrier             -
----------------------------------------------------------------------




More information about the Redhat-install-list mailing list