// fileDes2.c++ close stdout,open new file for output // $ ./a.out #include using namespace std; #include #include #include #include #include #include #include int main(int ac, char **av ) { // fileDes2.c++ int chPID, status, fd ; if(ac < 2){ cerr << "Less arguments\n"; return 0; } chPID = fork(); if(chPID == -1){ cerr << "fork() error\n"; return 0; } // if(chPID != 0) { // parent cout << "In parent\n"; waitpid(chPID, &status, 0) ; } else { // child cout << "In child\n"; if(close(fileno(stdout)) == -1){ cerr << "File close error" << endl; return 0; } fd = open(av[1], O_WRONLY | O_CREAT, 0666); if(fd == -1){ cerr << "File open error" << endl; return 0; } cout << "Again in child" << endl; close(fd); } return 0 ; }