학교 수업 및 과제/유닉스 프로그래밍
[UNIX 시스템 프로그래밍] 연습문제 2.7 풀이
goId
2018. 5. 30. 14:10
[UNIX 시스템 프로그래밍 second Edition - KEITH HAVILAND저] 연습문제 풀이입니다.
(저작권에 저촉 될 우려가 있어 문제 내용은 생략합니다.)
연습문제 2.7입니다.
파일 복사를 위해 한 파일을 모두 읽어들여 새로 만든 파일에 내용을 모두 덮어쓰는 방식으로 진행했습니다.
오류 처리 외에는 특별히 신경 쓸 부분이 없습니다.
코드는 C++로 작성하였습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <unistd.h> #include <fcntl.h> #define BUFSIZE 512 #define PERM 0644 int mycp (const char* name1, const char* name2) { int infile, outfile; ssize_t nread; char buffer[BUFSIZE]; if((infile = open(name1, O_RDONLY)) == -1) return (-1); if((outfile = open(name2, O_WRONLY|O_CREAT|O_TRUNC, PERM)) == -1) { close(infile); return (-2); } while((nread = read(infile, buffer, BUFSIZE)) > 0) { if(write(outfile, buffer, nread) < BUFSIZE) { close(infile); close(outfile); return (-3); } } close(infile); close(outfile); if(nread == -1) return (-4); else return (0); return 0; } int main(int argc, char* argv[]) { mycp(argv[1],argv[2]); return 0; } | cs |