/* redirect.c Jeff Ondich, 16 April 2002 Updated 17 February 2022 Modified by Tanya Amert for Fall 2024 This program illustrates the use of the dup2 function to redirect stdout to a file. What changes would you need to make to adapt this code to redirect stdin? */ #include #include #include #include #include #include #include // A simple example of printing one message to the original // stdout and another to somewhere else. int main() { // Before redirecting, printf should print to the default stdout printf("This should go to the screen (unless you used >, you sneaky devil!)\n"); fflush(stdout); // Let's open a file and redirect stdout there const char *file_name = "otter.txt"; int fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd < 0) { perror("Trouble opening file"); exit(1); } if (dup2(fd, STDOUT_FILENO) < 0) { perror("Trouble dup2-ing to stdout"); close(fd); // clean up open things before you go exit( 1 ); } close(fd); // do you understand what this line is for? // Finally, try printf again--it should go to the file printf("This goes to the newly redirected stdout.\n"); fflush(stdout); return 0; }