Redirection of stderr

Jonathan I. Kamens jik at athena.mit.edu
Fri Mar 15 08:06:50 AEST 1991


In article <574 at dprmpt.UUCP>, larry at dprmpt.UUCP (Larry) writes:
|> In a C program, how can I change stderr from wherever it is directed
|> via the command line to a file, then later revert it back to it's 
|> original destination?

  This is not a comp.unix.wizards question.  I have cross-posted this response
to comp.unix.questions and directed followups there.

  First, "stderr_fd = dup(fileno(stderr))" to preserve the original stderr
output direction.  Then "fclose(stderr)", and immediately do "stderr =
fopen(your_stderr_file, "w")" (the "immediately" is so that the newly opened
file will get the same file descriptor stderr had, in case you've got that
file descriptor hard-coded somewhere in your code).  When you're done,
"fclose(stderr)" and do "stderr = fdopen(stderr_fd, "w")" to put things back
the way they were originally.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik at Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710



More information about the Comp.unix.questions mailing list