Possible Duplicate:
redirecting output to a file in C

I'm running a process as follows:

char* [NUM];
char[0] = processName;
char[1] = arg0;
...
char[NUM] = 0;
execv(args[0],args);

The question is how I can make it redirect its output to

/dev/null

I tried adding it to the args array but it sent it as arguments to the program...

Any idea anyone?

Thanks

有帮助吗?

解决方案

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 ); 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top