Recent Posts
printf("ho_tari\n");
Chapter 1 (read) 본문
<low_read.c>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define BUF_SIZE 100
void error_handling(char* message);
int main(void)
{
int fd;
char buf[BUF_SIZE];
fd=open("data.txt", O_RDWR); //read, write 모두 사용가능
if( fd==-1)
error_handling("open() error!");
printf("file descriptor: %d \n" , fd);
if(read(0, buf, sizeof(buf))==-1) // 0 : 입력
error_handling("read() error!");
if(write(fd, buf, sizeof(buf))==-1)
error_handling("write() error!");
printf("file data: %s", buf);
close(fd);
return 0;
}
void error_handling(char* message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
/*
root@com:/home/swyoon/tcpip# gcc low_read.c -o lread
root@com:/home/swyoon/tcpip# ./lread
file descriptor: 3
file data: Let's go!
root@com:/home/swyoon/tcpip#
*/
<compile 결과>
입력 프롬프트가 뜬다.
'TCP IP 소켓 프로그래밍' 카테고리의 다른 글
Chapter 3 (address) (0) | 2023.10.10 |
---|---|
Chapter 3 (endian) (0) | 2023.10.10 |
Chapter 1 (read, server, client 통신) (0) | 2023.10.10 |
Chapter 1 (open) (0) | 2023.10.10 |
Chapter 1 (server, client 통신) (0) | 2023.10.10 |