It is currently Tue, 07 Dec 2021 02:28:25 GMT



 
Author Message
 Redirecting stdout of one program to my program
Hi!

How can I redirect the output (presumably on stdout) of a program that I
execute using exec() so that I can read it in my program?  I'm pretty sure
I've done this before (many moons ago in a class), but I can't remember how
do to it now!  Thanks!

Craig



 Fri, 07 Dec 2001 03:00:00 GMT   
 Redirecting stdout of one program to my program
     [courtesy cc of this posting mailed to cited author]

In comp.unix.programmer,
    "Craig L. Ching" <cch...@mqsoftware.com> writes:
:How can I redirect the output (presumably on stdout) of a program that I
:execute using exec() so that I can read it in my program?  I'm pretty sure
:I've done this before (many moons ago in a class), but I can't remember how
:do to it now!  Thanks!

The easiest way is to use the popen(3) function for reading.

     #include <stdio.h>

     FILE *fp;

     if (fp = popen("program arg here", "r")) {
        /* now use fgets or whatever on fp */
     }

--tom
--
If I had to choose between System V and 4.2, I'd resign. --Peter Honeyman



 Fri, 07 Dec 2001 03:00:00 GMT   
 Redirecting stdout of one program to my program
And what if I don't want the easy way?

news:376e6a32@cs.colorado.edu...



 Fri, 07 Dec 2001 03:00:00 GMT   
 Redirecting stdout of one program to my program
     [courtesy cc of this posting mailed to cited author]

In comp.unix.programmer,
    "Craig L. Ching" <cch...@mqsoftware.com> writes:
:And what if I don't want the easy way?

Then you learn to roll your own with fork, dup, exec, etc.

Actually, there are even easier ways, like backticks in the
shell or perl.

But the original poster was forgetting about 2>&1 for stderr
duplication.

--tom
--
Vote Cthulhu for President!  Why vote for the *lesser* evil?
                                - Bumper Sticker



 Fri, 07 Dec 2001 03:00:00 GMT   
 Redirecting stdout of one program to my program
Thanks for the help.  What I've decided on is to open a couple of pipes (via
pipe()), dup2() stdout and stderr to the pipe descriptors, and capture the
output in my program by doing reads on the pipes.  This will do exactly what
I need and it appears that dup2 was what I was looking for.  If anyone wants
me to post code, let me know!  Thanks!

Craig



 Fri, 07 Dec 2001 03:00:00 GMT   
 Redirecting stdout of one program to my program
Craig L. Ching (cch...@mqsoftware.com) wrote:
: Thanks for the help.  What I've decided on is to open a couple of pipes (via
: pipe()), dup2() stdout and stderr to the pipe descriptors, and capture the
: output in my program by doing reads on the pipes.  This will do exactly what
: I need and it appears that dup2 was what I was looking for.  If anyone wants
: me to post code, let me know!  Thanks!

This is how I'd normally do it:

pid_t run_process( int *in_pipe, int *out_pipe, int *err_pipe,
        char *prog, char **args )
{
        pid_t   pid;
        int     sin_pipe[2];
        int     sout_pipe[2];
        int     serr_pipe[2];

        if( pipe( sin_pipe ) < 0 ) {
                return (pid_t)-1;
        }
        if( pipe( sout_pipe ) < 0 ) {
                /* Close sin_pipe[0-1] */
                return (pid_t)-1;
        }
        if( pipe( serr_pipe ) < 0 ) {
                /* Close sin_pipe[0-1], sout_pipe[0-1] */
                return (pid_t)-1;
        }
        switch( (pid = fork()) ) {
        case -1:
                /* Close pipes... */
                return (pid_t)-1;
                break;
        case 0:
                /* Child */
                dup2( sin_pipe[0], 0 );
                dup2( sout_pipe[1], 1 );
                dup2( serr_pipe[1], 2 );
                /* ... checking for dup2() errors ... */
                close( sin_pipe[1] );
                close( sout_pipe[0] );
                close( serr_pipe[0] );
                execv( prog, args );    /* or appropiate choice */
                /* Close other halves of pipes.. */
                return (pid_t)-1;
                break;
        default:
                /* Parent */
                *in_pipe = sin_pipe[1];
                *out_pipe = sout_pipe[0];
                *err_pipe = serr_pipe[0];
                close( sin_pipe[0] );
                close( sout_pipe[1] );
                close( serr_pipe[1] );
                break;
        }
        return pid;

HTH,
  Fred.
--
+----------------------------------------------------------------------+
| Fred Barnes, CS Student, UKC                http://teddy.xylene.com/ |
| fr...@ukc.ac.uk                            http://stuE253.ukc.ac.uk/ |
+----------------------------------------------------------------------+



 Sat, 08 Dec 2001 03:00:00 GMT   
 
   [ 6 post ] 

Similar Threads

1. redirect interactive program's stdin, stdout

2. Redirect stdout after starting a program

3. Redirecting output of one command to both stdout and stderr

4. Redirect stdout * stderr to one file in bourne sh using exec

5. Redirecting stdout to More Than One Place

6. redirecting STDOUT of a bg process to STDOUT of a new xterm

7. drug affiliate program link pharmacies rx affiliate forum affiliate pharmacy program drug affiliate program

8. Running program inside kermit with stdin and stdout connected modem

9. calling a program from c++ and pipe stdout to stdin

10. Program not writing to stdout


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software