Skip to content

Instantly share code, notes, and snippets.

Last active Dec 27, 2019
What would you like to do?
dup2 usage
// A simple example of how dup2 can be used
const char* msg = "Text written by standard file descriptor\n";
int fd = open("dup2_test.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
write(fd, msg, strlen(msg));
/* Map stdout to our file */
dup2(fd, STDOUT_FILENO);
printf("Text written by dup @ stdout file descriptor\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment