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
- File 조작
- For Beginners
- c++
- 시스템 프로그래밍
- 5622
- 브런치
- 전자책
- Process Communication
- LJESNJAK
- 시프
- 백준
- 10773
- 균형잡힌 세상
- process control
- BAKA
- 바샤
- IT
- 1874
- 입력 버퍼
- c
- 해바
- Baekjoon
- Zero That Out
- 2941
- 4949
- QA
- Parenthesis
- file IO
- system programming
- The Balance of the World
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