Last active
July 20, 2017 22:50
-
-
Save hamadu/c45d9f06202953deff05bb84f8000d61 to your computer and use it in GitHub Desktop.
copy.c
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 <unistd.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
void error_and_exit() { | |
printf("error(%d): %s\n", errno, strerror(errno)); | |
exit(EXIT_FAILURE); | |
} | |
int main(int argc, char* argv[]) { | |
int srcFd = open("./src.txt", O_RDONLY); | |
if (srcFd == -1) { | |
error_and_exit(); | |
} | |
int dstFd = open("./dst.txt", O_WRONLY | O_CREAT); | |
if (dstFd == -1) { | |
error_and_exit(); | |
} | |
char buffer[16]; | |
while (1) { | |
int num = read(srcFd, buffer, 16); | |
if (num == 0) { | |
break; | |
} | |
write(dstFd, buffer, num); | |
} | |
close(srcFd); | |
close(dstFd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment