반응형

closedir(3)

#include <sys/types.h>
#include <dirent.h>

int closedir(DIR *dirp);

opendir(3)으로 open한 DIR *에 대해, 더 이상 directory의 파일목록을 검색을 하지 않을 경우에 closedir(3)로 DIR *를 close한다.

 

 

파라미터

dirp
    - opendir(3) 또는 fdopendir(3)을 통하여 생성된 DIR *

 

RETURN

-1
   - 오류가 발생하였으며, 상세한 오류는 errno에 저장됩니다.
   - EBADF : dirp가 DIR *로서 유효하지 않습니다.

0
   - 정상적으로 close하였습니다.

 


활용 예제

 

 

Sample). 로그인한 home directory의 전체 파일 목록 출력

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h> 


int main(int argc, char **argv)
{
    DIR           *dir_ptr = NULL;
    struct dirent *file    = NULL;
    char           home[1024];

    strncpy(home, getenv("HOME"), sizeof(home));

    /* 목록을 읽을 디렉토리명으로 DIR *를 return 받습니다. */
    if((dir_ptr = opendir(home)) == NULL) 
    {
        fprintf(stderr, "%s directory 정보를 읽을 수 없습니다.\n", home);
        return -1;
    }

    /* 디렉토리의 처음부터 파일 또는 디렉토리명을 순서대로 한개씩 읽습니다. */
    while((file = readdir(dir_ptr)) != NULL) 
    {
        /*
        *   struct dirent *의 구조체에서 d_name 이외에는 
        *   시스템마다 항목이 없을 수 있으므로 무시하고 이름만 사용합니다.
        */

        printf("%s\n", file->d_name);
    }

    /* open된 directory 정보를 close 합니다. */

    closedir(dir_ptr);
    
    return 0;
}

 


 

see also :

    File 속성 정보 및 파일 관리 Library

    Directory 정보 조회 및 관리 Library

    System Call File I/O Library

    Stream File I/O Library

 

 

 

반응형
블로그 이미지

자연&사람

행복한 개발자 programmer since 1995.

,