함수이름 | 설명 |
---|---|
open | 파일을 읽거나 쓸거나 생성하며 파일을 오픈한다. |
create | 빈 파일을 생성한다. |
close | 전에 열려있던 파일을 닫는다. |
read | 해당 파일의 내용을 읽어 추출한다. |
write | 해당 파일의 내용을 어딘가에 쓰거나 출력한다. |
lseek | 해당 파일의 원하는 위치로 이동한다. |
remove | 파일을 제거한다. |
파일 디스크립터값은 음수가 아닌 수로 모든 파일에게는 부여되는데 이미 존재하는 파일을 열거나 새로운 파일을 생성할 때 이 값을 리턴한다.
0-2는 이미 할당된 번호이다.
file descriptor | sysbolic constant | describe |
---|---|---|
0 | STDIN_FILENO | Standard input |
1 | STDOUT_FILENO | Standard output |
2 | STDERR_FILENO | Standard error |
#include <fcntl.h>
#include <unistd.h>
main()
{
int fd;
/*ssize_t는 signed size type으로 32bit인 int이다. 입출력 실패시 -1을 반환하는데
이 값의 타입이 ssize_t이므로 주로 입출력함수의 반환타입으로 설정된다.*/
ssize_t nread;
char buf[1024];
fd = open(“data”, O_RDONLY); //data라는 파일을 읽기 전용으로 연다. 이때 이 파일의 file descriptor값이 반환된다.
nread = read(fd, buf, 1024); //data 파일을 읽은 후 읽은 byte 수를 반환한다.
close(fd); //date 파일을 닫는다.
/*자세한 설명은 일단 생략.*/
}
#include <fcntl.h>
int open(const char *pathname, int flags [, mode_t mode]);
/*성공시 file descriptor 반환, 실패시 -1 반환
fd = open(“/tmp/newfile”, O_WRONLY|O_CREAT, 0644);
/* if isExist(file) “file open” else “file create & open” */
fd = open(“/tmp/newfile”, O_WRONLY|O_CREAT|O_EXCL, 0644);
/* if isExist(file) “open error” else “file create & open” */
fd = open(“/tmp/newfile”, O_WRONLY|O_CREAT|O_TRUNC, 0644);
/* if isExist(file) “file truncate & open ” else “file create & open” */
#include <stdlib.h> //exit() 이용
#include <fcntl.h> //open() 이용
char *file = “firstfile”;
main()
{
int fd;
if( (fd = open(file, O_RDWR)) == -1) //open 실패시
{
printf(“Couldn’t open %s\n”, file);
exit(1);
}
exit(0);
}
#include <fcntl.h>
int creat(const char *pathname [, mode_t mode]);
/*성공시 fd가 반환되고 실패시 -1이 반환된다.*/
#include <unistd.h>
int close(int fd);
//성공시 0을 반환하고, 실패시 -1을 반환한다.
#include <unistd.h>
ssize_t read(int fd, void *buffer, size_t n);
//성공시 읽은 바이트의 수를 반환하고, EOF일 경우 0이 반환되고, 실패시 -1을 반환하다.
📗 예시
int fd;
ssize_t n1, n2, n3;
char buf1[512], buf2[512], buf3[512];
if( (fd = open(“test”, O_RDONLY)) == -1)
return -1;
//f_offset = 0
n1 = read(fd, buf1, 512); //n1 = 512, 512byte 읽음,f_offset = 512
n2 = read(fd, buf2, 512); //n2 = 88, 55byte 읽음, f_offset = 600
n3 = read(fd, buf3, 512); //n3 = 0, EOF
#include <unistd.h>
ssize_t write(int fd, const void *buffer, size_t n);
//성공시 작성한 byte만큼 반환되고, 실패시 -1이 반환된다.
📗 예시 - copy file
int copyfile ( const char *name1, const char *name2){
int infile, outfile;
ssize_t nread;
char buffer[512];
/*infile 읽기오류*/
if ( (infile = open (name1, O_RDONLY ) )== -1)
return(-1);
/*outfile 읽기오류*/
if ( (outfile = open (name2, O_WRONLY|O_CREAT|O_TRUNC, 644) )== -1){
close (infile);
return (-2);
}
/* nread값이 0이되면 EOF이라는 뜻, EOF까지 파일을 계속 읽음*/
while ( (nread = read (infile, buffer, 512) ) > 0){
/*infile의 값을 buffer에 복사*/
/*buffer에 복사된 내용을 다시 outfile에 복사*/
if ( write(outfile, buffer, nread) < nread ){
close (infile);
close (outfile);
return (-3); //쓰기오류
}
}
close (infile);
close (outfile);
if ( nread == -1) return (-4) //마지막 읽기오류
else return (0); //정상처리
}
int main(){
copyfile("test.in","test.out");
}
#include <unistd.h>
off_t lseek(int fd, off_t offset, int start_flag);
//성공시 새로운 파일의 offset이, 실패시 -1이 반환된다.
📗 예시 - 위와 아래는 같은 과정이다.
fd = open(test, O_RDWR);
lseek(fd, (off_t)0, SEEK_END);
write(fd, buf, n);
fd = open(test, O_WRONLY|O_APPEND);
write(fd, buf, n);