First open the target you want, then close the fd
you want to
reassign, use dup2
to connect it there, and close the previous open:
int tmpFd = open( "/dev/null", O_WRONLY );
if ( tmpFd == -1 ) {
// Real problem, couldn't open /dev/null
}
if ( dup2( tmpFd, 1 ) ) != 1 ) {
// Real problem, dup2 failed.
}
close( tmpFd );