redirecting output

Jason C Austin jason at cs.odu.edu
Fri Jun 29 05:20:10 AEST 1990


In article <22931 at dartvax.Dartmouth.EDU> emf at eleazar.dartmouth.edu (Eric Friets) writes:
-> 
-> I need to redirect output in the middle of a program
-> from the standard output to a file.  The use of
-> fprintf is ruled out because I do not have the
-> source of the functions whose output I want.
-> 
-> Options that are not acceptable:
-> 1. redirect output of the entire program, as in
-> 	prog > file
-> 2. have the rest of the program write to stderr, and 
-> then redirect the entire program, as above
-> 3. write the necessary data to a file, and have a
-> smaller program that redirects to a file
-> 
-> What is really needed is a way to change the pointer
-> to stdout to a file pointer returned by fopen().  Any
-> suggestions would be appreciated.  I will summarize and
-> post responses that are mailed to me.
-> 
-> Eric Friets
-> emf at eleazar.dartmouth.edu
-> .

	freopen will do the job for you.  In fact the unix manual page
says that this command is mostly used for this purpose.

	freopen( "filename", "w", stdout );

	Anything sent to stdout after this command is executed will go
to filename.  The function returns the previous value of stdout, so
you'll probably want to keep it and restore the value, if you want to
start sending things back to the screen.

	

	
--
Jason C. Austin
jason at cs.odu.edu



More information about the Comp.lang.c mailing list