Last active
February 2, 2023 03:48
-
-
Save dulimarta/88f54b44f14caae29900da2d735e14b1 to your computer and use it in GitHub Desktop.
CS452 Lab04 dup2()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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