Sorry, I made a slight mistake...
dup( fd, 1 ); ----> dup2( fd, 1 )
And an example code for dup2 is
/* dup2 sample */
#include <stdio.h>
#include <io.h>
int main( void )
{
FILE* fp = fopen( "myfile", "w" );
if( fp ) {
/* reserve old stdout */
int oldstdout = dup(1);
/* redirect stdout */
dup2( fileno(fp), 1 );
/* the following message will be written in the file 'myfile' */
puts( "My favorite musician is Freddie Mercury..." );
/* flush intermediate buffer for standard output */
fflush( stdout );
fclose( fp );
/* recover old stdout */
dup2( oldstdout, 1 );
/* the following message will be printed in the screen */
puts( "He is the greatest musician!" );
}
return 0;
}
/* end of source */
You can check the return value of dup or dup2 because it returns -1 if it is failed.
I wonder this function can help you...
Hee S. Chung
heesc@netian.com