redirecting standard output and standard error

redirecting standard output and standard error

Post by Chri » Wed, 20 Feb 2002 15:10:36



Hi,

I want to call a command using C programmatically using execv, but I
also want to capture the standard output and standard error to files I
specify through command line args.  I want to use the file descriptors
0, 1, and 2 appropriately for this, and I understand I should probably
call a fork()
when calling the execv so my program can return successfully.  But
from there,
I'm not sure where to start.  I think I want to probably use dup2 so I
can redirect the descriptors to files I open.  Maybe create a pipe
from the child process to the parent process, where I pass through the
pipe are the results of the command call ??  Just need some guidance
and want to make sure I'm in the right direction.  Thanks.

 
 
 

redirecting standard output and standard error

Post by Jack Klei » Wed, 20 Feb 2002 15:35:33



comp.lang.c:

Quote:> Hi,

> I want to call a command using C programmatically using execv, but I
> also want to capture the standard output and standard error to files I
> specify through command line args.  I want to use the file descriptors
> 0, 1, and 2 appropriately for this, and I understand I should probably
> call a fork()
> when calling the execv so my program can return successfully.  But
> from there,
> I'm not sure where to start.  I think I want to probably use dup2 so I
> can redirect the descriptors to files I open.  Maybe create a pipe
> from the child process to the parent process, where I pass through the
> pipe are the results of the command call ??  Just need some guidance
> and want to make sure I'm in the right direction.  Thanks.

Please don't cross-post platform specific, non standard issues to
comp.lang.c.  You're just plain off-topic here, as none of execv(),
file descriptors, dup2(), fork(), pipes, or processes are defined or
supported by the standard C language.  They are extensions provided by
your particular compiler/OS combination.

Hint, in the future check the man pages.  If they don't say that the
function/data type/feature is ANSI, don't post it here.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

 
 
 

redirecting standard output and standard error

Post by Fletcher Glen » Thu, 21 Feb 2002 01:24:51


What you want to do is an freopen() of the stdin, stdout, and
stderr after you fork() and before you exec().

--
Fletcher Glenn
to reply remove NOSPAM from my reply address


Quote:> Hi,

> I want to call a command using C programmatically using execv, but I
> also want to capture the standard output and standard error to files I
> specify through command line args.  I want to use the file descriptors
> 0, 1, and 2 appropriately for this, and I understand I should probably
> call a fork()
> when calling the execv so my program can return successfully.  But
> from there,
> I'm not sure where to start.  I think I want to probably use dup2 so I
> can redirect the descriptors to files I open.  Maybe create a pipe
> from the child process to the parent process, where I pass through the
> pipe are the results of the command call ??  Just need some guidance
> and want to make sure I'm in the right direction.  Thanks.