- buf에 있는 n바이트만큼의 데이터를 fd가 나타내는 파일에 쓴다
- prototype
#include <unistd.h>
ssize_t write(int fd, void *buf, size_t nbytes);
성공 시 실제 쓰여진 바이트 수, 실패 시 -1 리턴
- copy.c
/*파일 복사 프로그램*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#define BUFSIZE 512
main(int argc, char *argv[]) {
int fd1, fd2, n;
char buf[BUFSIZE];
if (argc != 3) {
fprintf(stderr, "사용법 : %s file1 file2\n", argv[0]);
exit(1);
}
if ((fd1 = open(argv[1], O_RDONLY)) == -1) {
perror(argv[1]);
exit(2);
}
if ((fd2 = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644) == -1) {
perror(argv[2]);
exit(3);
}
while((n=read(fd, buf, BUFSIZE))>0)
write(fd2, buf, n);
exit(0);
}