Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 균형잡힌 세상
- 시스템 프로그래밍
- 브런치
- c
- system programming
- The Balance of the World
- Zero That Out
- IT
- File 조작
- 백준
- 전자책
- process control
- 4949
- LJESNJAK
- QA
- 5622
- Baekjoon
- Process Communication
- BAKA
- Parenthesis
- 1874
- 바샤
- For Beginners
- 해바
- 2941
- 입력 버퍼
- 10773
- c++
- 시프
- file IO
Archives
- Today
- Total
해바
Lab #1 본문
open(), close(), read(), write() 시스템 호출을 이용하여, 한 파일의 내용을 다른 파일에 복사하는 copy.c 프로그램을 작성하라.
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFSIZE 512
#define PERMS 0644
int main(int argc, char** argv) {
char buffer[BUFSIZE];
int ofd, nfd; // ofd : 원본파일, nfd : 생성할 파일
ssize_t nread;
if((ofd = open(argv[1], O_RDONLY)) == -1) {
printf("Cannot open %s\n", argv[1]);
exit(1);
}
// 원본파일 열기
if((nfd = open(argv[2], O_RDWR|O_CREAT, PERMS)) == -1) {
printf("Cannot create %s\n", argv[2]);
exit(1);
}
// 복사할 파일 생성
while((nread = read(ofd, buffer, BUFSIZE)) > 0) {
write(nfd, buffer, nread);
}
// 파일 복사
close(ofd);
close(nfd);
exit(0);
}
컴파일
gcc copy.c
실행 :
./a.out
Comments