- 정규 파일뿐만 아니라, 디바이스, 소켓, 메모리, 프로세스 모두 파일로 표현됨
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define MAX_BUF 64
int main(int argc, char * argv[]){
int fd, read_size, write_size;
char buf[MAX_BUF];
if(argc !=2)
{
printf("USAGE: %s file name\n", argv[0]);
exit(-1);
}
fd = open(argv[1], O_RDONLY);
if(fd<0)
{
printf("Can't open file %s", argv[1]);
exit(-1);
}
while(1){
read_size= read(fd,buf,MAX_BUF);
if(read_size==0){
break;
}
write_size = write(STDOUT_FILENO,buf,read_size);
}
close(fd);
return 0;
}
>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define BUF_SIZE 1024
int main(int argc, char* argv[])
{
int fd, rfd, read_size, write_size;
char buf[BUF_SIZE];
if (argc != 3)
{
perror("argu");
exit(1);
}
fd = open(argv[1], O_RDONLY);
if (fd < 0)
{
perror("openfile");
exit(1);
}
rfd = open(argv[2], O_RDWR | O_CREAT, 0641);
dup2(rfd, STDOUT_FILENO);
while (1)
{
read_size = read(fd, buf, BUF_SIZE);
if (read_size == 0)
break;
write_size = write(STDOUT_FILENO, buf, read_size);
}
close(fd);
close(rfd);
return 0;
}