#include #include #include #include #include #include using namespace std; int main () { int flags = O_WRONLY | O_TRUNC | O_CREAT; mode_t mode = S_IRUSR | S_IWUSR; int fd{open("outputFile", flags, mode)}; if (-1 == fd) { perror("Error open: "); return 1; } // make stdout point to the file. if ( -1 == dup2(fd, STDOUT_FILENO )) { perror("Error dup2: "); } close(fd); for(int i =0; i < 10; ++i) { cout << "i = " << i << endl; } return 0; }