Skip to content

Instantly share code, notes, and snippets.

@aakbar5
Last active December 27, 2019 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aakbar5/cd33daa5cc4093bdead46a069abd71b9 to your computer and use it in GitHub Desktop.
Save aakbar5/cd33daa5cc4093bdead46a069abd71b9 to your computer and use it in GitHub Desktop.
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