/* * dupTOstdout1.c++ dup() an open file to the slot of stdout * $ ./a.out dupOut */ #include using namespace std; #include #include #include #include #include #include int main(int ac, char *av[]){ int fd1; // dupTOstdout1.c++ // $ ./a.out dupOut if(ac < 2){ cerr << "File name not specified\n"; exit(1); } fd1 = open(av[1], O_CREAT | O_WRONLY, 0666); if(fd1 == -1){ cerr << "File open error\n"; exit(1); } cout << "Line before close(fileno(stdout))\n"; close(fileno(stdout)); cout << "Line after close(fileno(stdout))\n"; dup(fd1); cout << "Line after dup(fd1)\n"; close(fd1); return 0; }