redirecting output

Eric Friets emf at eleazar.dartmouth.edu
Sat Jul 28 01:17:13 AEST 1990


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 at eleazar.dartmouth.edu
.



More information about the Comp.lang.c mailing list