학교 수업 및 과제/유닉스 프로그래밍
[UNIX 시스템 프로그래밍] 연습문제 2.14 풀이
goId
2018. 5. 30. 14:17
[UNIX 시스템 프로그래밍 second Edition - KEITH HAVILAND저] 연습문제 풀이입니다.
(저작권에 저촉 될 우려가 있어 문제 내용은 생략합니다.)
연습문제 2.14입니다.
두 번째 인수에서 r, w, rw, a 문자(열)만 체크해서 그에 따른 적절한 처리를 해 주면 됩니다.
문자를 하나씩 받아오면 예외 처리 할 부분이 많아져 string으로 받아와 strcmp 함수를 통해 적절히 처리하였습니다.
코드는 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 | #include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <string.h> int fileopen(char* filename, char* filetype) { int filedes; printf("%s\n",filetype); if(!strcmp(filetype,"r")) filedes = open(filename, O_RDONLY); else if(!strcmp(filetype,"rw")) filedes = open(filename, O_RDWR); else if(!strcmp(filetype,"w")) filedes = open(filename, O_WRONLY); else if(!strcmp(filetype,"a")) filedes = open(filename, O_WRONLY | O_APPEND); else { printf("cannot find filetype\n"); filedes = -1; return (-1); } printf("ok\n"); return filedes; } int main(int argc, char* argv[]) { int filedes = fileopen(argv[1], argv[2]); printf("file discripter is %d\n",filedes); return 0; } |