시스템에서 정의하는 자료형의 이름에 _t가 붙어 있는 이유
: 이는 primitive 자료형을 의미하며 일반적으로 sys/type.h에 typedef 선언을 통해 정의되어 있다. 기본 자료형에 다른 이름을 붙여논 것으로 시스템이 변경됨에 따라 자료형의 표현 방식이 달라질 수 있는데 이 때, typedef 선언만 변경해 코드의 변경을 최소화 할 수 있기 때문이다.
int open(const char *path, int flag);
//성공 시 파일 디스크립터, 실패 시 -1 반환
int close(int fd);
//성공 시 0, 실패 시 -1 반환
ssize_t write(int fd, const void *buf, size_t nbytes);
//파일 디스크립터, 버퍼의 주소 값, 바이트 수 전달
ssize_t read(int fd, void *buf, size_t nbytes);
//성공 시 수신한 바이트 수, 실패 시 -1 반환
low_open.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
void error_handling(char* message);
int main(void)
{
int fd;
char buf[]="Let's go!\n";
fd=open("data.txt", O_CREAT|O_WRONLY|O_TRUNC);
if(fd==-1)
error_handling("open() error!");
printf("file descriptor: %d \n", fd);
if(write(fd, buf, sizeof(buf))==-1)
error_handling("write() error!");
close(fd);
return 0;
}
void error_handling(char* message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
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_RDONLY);
if( fd==-1)
error_handling("open() error!");
printf("file descriptor: %d \n" , fd);
if(read(fd, buf, sizeof(buf))==-1)
error_handling("read() error!");
printf("file data: %s", buf);
close(fd);
return 0;
}
void error_handling(char* message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
이론만 보고 넘어간다.