<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.2802" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr align=left><FONT face=Arial color=#0000ff
size=2></FONT> </DIV><BR>
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> redhat-install-list-bounces@redhat.com
[mailto:redhat-install-list-bounces@redhat.com] <B>On Behalf Of
</B>mike<BR><B>Sent:</B> Wednesday, January 11, 2006 3:24 PM<BR><B>To:</B>
Getting started with Red Hat Linux<BR><B>Subject:</B> piping standard input to a
program?<BR></FONT><BR></DIV>
<DIV></DIV>
<DIV>so i have a program that takes standard input as input. it's run from
a shell like so:</DIV>
<DIV> </DIV>
<DIV>echo -e "blah blah blah" | <program> <options> </DIV>
<DIV> </DIV>
<DIV>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?<SPAN
class=062242101-12012006><FONT face=Arial color=#0000ff
size=2> </FONT></SPAN></DIV>
<DIV><SPAN class=062242101-12012006></SPAN> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2>This isn't the best place for a programming question, but here's a sample
to</FONT></SPAN></DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2>get you thinking.</FONT></SPAN></DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=062242101-12012006><FONT color=#0000ff><FONT face="Courier New"
size=2>This was found at: </FONT><FONT face="Courier New" size=2><A
href="http://www.metalshell.com/view/source/116/">http://www.metalshell.com/view/source/116/</A></FONT><A
href="http://linuxgazette.net/issue24/rogers.html"><FONT face="Courier New"
size=2></FONT></A></FONT></SPAN></DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2>Basic programming like this will get you started. And there are plenty of
sites</FONT></SPAN></DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2>with this info.</FONT></SPAN></DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2>Get it running. Break it. Change it.</FONT></SPAN></DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2>This is a console application.</FONT></SPAN></DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2>The input options you ask about are arguments to the program.
They</FONT></SPAN></DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2>use argc and argv.</FONT></SPAN></DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2> argc = number of arguments to the program</FONT></SPAN></DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2> argv = values of arguments</FONT></SPAN></DIV>
<DIV><SPAN class=062242101-12012006></SPAN> </DIV>
<DIV><SPAN class=062242101-12012006><FONT color=#0000ff><FONT face="Courier New"
size=2>/* openreaddir.c by mind [mind@metalshell.com]<BR> *<BR> *
Example on using opendir, closedir, and readdir to open a directory<BR> *
stream and read in and print file names.<BR> *<BR> *
06/04/03<BR> *<BR> * </FONT><A href="http://www.metalshell.com/"><FONT
face="Courier New" size=2>http://www.metalshell.com/</FONT></A><BR><FONT
face="Courier New" size=2> *<BR> */</FONT></FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2>#include <stdio.h><BR>#include <sys/types.h><BR>#include
<dirent.h><BR>#include <errno.h></FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2>int main(int argc, char
*argv[])<BR>{<BR>
DIR
*dip;<BR> struct dirent
*dit;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2>
int i =
0;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2> /* check to see if user
entered a directory name */<BR> if
(argc < 2)<BR>
{<BR>
printf("Usage: %s <directory>\n",
argv[0]);<BR>
return 0;<BR> }</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2> /* DIR *opendir(const char
*name);<BR>
*<BR> * Open a directory stream
to argv[1] and make sure<BR> *
it's a readable and valid (directory)
*/<BR> if ((dip = opendir(argv[1])) ==
NULL)<BR>
{<BR>
perror("opendir");<BR>
return 0;<BR> }</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2> printf("Directory stream is
now open\n");</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2> /* struct dirent
*readdir(DIR *dir);<BR>
*<BR> * Read in the files from
argv[1] and print */<BR> while ((dit =
readdir(dip)) != NULL)<BR>
{<BR>
i++;<BR>
printf("\n%s", dit->d_name);<BR>
}</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2> printf("\n\nreaddir() found a
total of %i files\n", i);</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2> /* int closedir(DIR
*dir);<BR>
*<BR> * Close the stream to
argv[1]. And check for errors. */<BR>
if (closedir(dip) == -1)<BR>
{<BR>
perror("closedir");<BR>
return 0;<BR> }</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=062242101-12012006><FONT face="Courier New" color=#0000ff
size=2> printf("\nDirectory stream is
now closed\n");<BR> return
1;<BR>}</FONT></SPAN></DIV></BODY></HTML>