I am wondering how to make dup2 to stop writing to a file even after i closed the file when i use close(fd). For instance the last line is still writing to the file, but i want it to write to the console. How would i do this?
Code:
#include <stdio.h>
#include <fcntl.h>
main()
{
int fd;
char *s;
fd = open("file4", O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (dup2(fd, 1) < 0) { perror("dup2"); exit(1); }
printf("Standard output now goes to file4\n");
close(fd);
printf("It goes even after we closed file descriptor %d\n", fd);
putchar('p');
putchar('u');
putchar('t');
putchar('c');
putchar('h');
putchar('a');
putchar('r');
putchar(' ');
putchar('w');
putchar('o');
putchar('r');
putchar('k');
putchar('s');
putchar('\n');
s = "And fwrite\n";
fwrite(s, sizeof(char), strlen(s), stdout);
fflush(stdout);
s = "And write\n";
write(1, s, strlen(s));
printf("It goes even after we closed file descriptor %d\n", fd);
}