예제 |
#include <stdio.h> // puts()
#include <string.h> // strlen(), memset()
#include <fcntl.h> // O_RDWR, O_CREAT
#include <unistd.h> // write(), close(), dup()
#define BUFF_SIZE 1024
int main()
{
int fd_wr;
int fd_rd;
char buff[BUFF_SIZE];
char *str = "forum.falinux.com";
fd_wr = open( "./test.txt", O_RDWR ¦ O_CREAT ¦ O_TRUNC, 0644);
fd_rd = dup2( fd_wr, 100);
printf( "fd_wr= %d fd_rd= %d\n", fd_wr, fd_rd); // 디스크립터 번호 확인
write( fd_wr, str, strlen( str));
close( fd_wr); // fd_wr 닫음
lseek( fd_rd, 0, SEEK_SET);
memset( buff, '\0', BUFF_SIZE);
read( fd_rd, buff, BUFF_SIZE);
printf( "%s\n", buff);
close( fd_rd); // fd_rd 닫음
return 0;
}
]$ ./a.out
fd_wr= 3 fd_rd= 100
forum.falinux.com
]$ |
RECENT COMMENT