Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

<a href="/modules/moniwiki/wiki.php/manSearch?google=none&name=close">close</a>(2)

1장. close(2)

차례
1.1절. 사용법
1.2절. 설명
1.3절. 반환값
1.4절. 예제

파일지정번호를 닫는다.


1.1절. 사용법

#include <unistd.h>

int close(int fd);
		


1.2절. 설명

close 는 열린 파일 지정번호를 닫는다. 열린파일을 다시 참조하지 않고자 할때 사용될수 있다. 만약 열린파일이 더이상 참조되지 않는다면 close 를 이용해서 닫아주어야 한다.


1.3절. 반환값

성공할경우 0을 실패했을경우에는 -1을 반환하며, 적당한 errno 값을 설정한다.


1.4절. 예제

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    int fd;

    if((fd = open("test.txt",O_CREAT|S_IRUSR)) < 0)
    {
        perror("file open error : ");
        exit(0);
    }

    // 파일과 연관된 각종 작업을한다.  
    // ... 
    // ...

    close(fd);
    exit(0);
}