Skip to content

Instantly share code, notes, and snippets.

@aakbar5
Last active Dec 27, 2019
Embed
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");
close(fd);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment