Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!snorkelwacker!mit-eddie!bu.edu!dartvax!eleazar.dartmouth.edu!emf From: emf@eleazar.dartmouth.edu (Eric Friets) Newsgroups: comp.lang.c Subject: redirecting output Message-ID: <23395@dartvax.Dartmouth.EDU> Date: 27 Jul 90 15:17:13 GMT Sender: news@dartvax.Dartmouth.EDU Distribution: na Organization: Dartmouth College, Hanover, NH Lines: 32 A while ago I posted a request concerning redirecting output in midprogram. Thanks for the many responses; the only workable one (received from a few netters) looks something like this: (the key is the dup2...) int fdstdout,fdplot; fflush(stdout); fdplot=fopen("plot.out",O_WRONLY|O_CREAT,0644); fdstdout=dup(1); dup2(fdplot,1); ... ... ... fflush(&_iob[fdplot]); dup2(fdstdout,1); close(fdplot); close(fdstdout); This works, but has a small glitch. When unredirecting the output (ie, sending to stdout instead of the file), a few characters at the end of the file are corrupted. Running the program without this redirection results in the correct output. I tried pausing the program for a few seconds before unredirecting, but this had no effect. The computer is a SUN 3/160, running under the UNIX operating system. Thanks, Eric Friets emf@eleazar.dartmouth.edu .