piping standard input to a program?

Bret Stern bret_stern at machinemanagement.com
Thu Jan 12 01:31:46 UTC 2006


 

  _____  

From: redhat-install-list-bounces at redhat.com
[mailto:redhat-install-list-bounces at redhat.com] On Behalf Of mike
Sent: Wednesday, January 11, 2006 3:24 PM
To: Getting started with Red Hat Linux
Subject: piping standard input to a program?


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? 
 
 
This isn't the best place for a programming question, but here's a sample to
get you thinking.
 
This was found at:  http://www.metalshell.com/view/source/116/
<http://linuxgazette.net/issue24/rogers.html> 
 
Basic programming like this will get you started. And there are plenty of
sites
with this info.
Get it running. Break it. Change it.
This is a console application.
 
The input options you ask about are arguments to the program. They
use argc and argv.
 
  argc = number of arguments to the program
  argv = values of arguments
 
/* openreaddir.c by mind [mind at metalshell.com]
 *
 * Example on using opendir, closedir, and readdir to open a directory
 * stream and read in and print file names.
 *
 * 06/04/03
 *
 *  <http://www.metalshell.com/> http://www.metalshell.com/
 *
 */
 
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
 
int main(int argc, char *argv[])
{
        DIR             *dip;
        struct dirent   *dit;
 
        int             i = 0;
 
        /* check to see if user entered a directory name */
        if (argc < 2)
        {
                printf("Usage: %s <directory>\n", argv[0]);
                return 0;
        }
 
        /* DIR *opendir(const char *name);
         *
         * Open a directory stream to argv[1] and make sure
         * it's a readable and valid (directory) */
        if ((dip = opendir(argv[1])) == NULL)
        {
                perror("opendir");
                return 0;
        }
 
        printf("Directory stream is now open\n");
 
        /*  struct dirent *readdir(DIR *dir);
         *
         * Read in the files from argv[1] and print */
        while ((dit = readdir(dip)) != NULL)
        {
                i++;
                printf("\n%s", dit->d_name);
        }
 
        printf("\n\nreaddir() found a total of %i files\n", i);
 
        /* int closedir(DIR *dir);
         *
         * Close the stream to argv[1]. And check for errors. */
        if (closedir(dip) == -1)
        {
                perror("closedir");
                return 0;
        }
 
        printf("\nDirectory stream is now closed\n");
        return 1;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/redhat-install-list/attachments/20060111/dea4e819/attachment.htm>


More information about the Redhat-install-list mailing list