Skip to content

Instantly share code, notes, and snippets.

@dulimarta
Last active February 2, 2023 03:48
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 dulimarta/88f54b44f14caae29900da2d735e14b1 to your computer and use it in GitHub Desktop.
Save dulimarta/88f54b44f14caae29900da2d735e14b1 to your computer and use it in GitHub Desktop.
CS452 Lab04 dup2()
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
int main() {
char name[50];
int fd = open("myoutput.txt",
O_WRONLY | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR);
if (errno != 0) {
perror("Unable to create myoutput.txt");
return 0;
}
printf ("Enter your name: ");
fgets(name, 50, stdin);
printf ("You entered: %s\n", name);
printf ("Enter your neigbhor's name: ");
fgets(name, 50, stdin);
dup2(fd, fileno(stdout));
printf ("Your neighbor is: %s\n", name);
close (fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment