use of dup2

How can i use dup2 command to write the output of execl command to a file named "newfile.txt"??
# i am using sed command inside execl
Before you fork, create a new file descriptor via open() to newfile.txt.

After you fork and before you execl, in the child process, close stdin and stdout.

Then dup2( fd, 1 ); dup2( fd, 2 );

Then execl.

Now stdout and stderr of child process will be redirected to newfile.txt
Topic archived. No new replies allowed.