#include<stdio.h>
#include<unistd.h>
int main(){
int fd[2];
int old = dup(1);
char buf[10];
pipe(fd);
dup2(fd[1],1);
printf("Don't Print!\n");
dup2(old,1);
printf("Hello World!\n");
return 0;
}
In the above C code "Don't Print" also gets printed.
dup2(fd[1],1)
Sets the default output to the pipe. So printf should write to the output which in this case is the pipe.
dup2(old,1)
if remove this then nothing is printed to the screen but when I include it both strings are printed to the screen. Can someone point out the mistake in my code.