반응형

telldir(3)

#include <dirent.h>

long telldir(DIR *dirp);

디렉토리의 정보를 읽을 현재 위치를 얻습니다.

 

 

파라미터

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

 

RETURN

0 이상
    - 디렉토리 정보를 읽을 현재 위치 정보


-1
    - 오류가 발생하였으며, 오류 내용은 errno에 설정되었습니다.
    
    EBADF : dirp가 DIR *로서 유효하지 않습니다.

 

 


활용 예제

 

Sample). 

#include <stdio.h>
#include <string.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];
    long           location;

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

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

    location = 0L;

    /* 디렉토리의 처음부터 파일 또는 디렉토리명을 순서대로 한개씩 읽습니다. */
    while((file = readdir(dir_ptr)) != NULL) 
    {
        if(strcmp(file->d_name, "myfile.txt") == 0) {
            location = telldir(dir_ptr);
        }
        printf("%s\n", file->d_name);
    }

    seekdir(dir_ptr, location);

    /* myfile.txt 파일 다음부터 처리를 다시 함... */

    //......

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

    closedir(dir_ptr);
}

 


see also :

    File 속성 정보 및 파일 관리 Library

    Directory 정보 조회 및 관리 Library

    System Call File I/O Library

    Stream File I/O Library

 

 

 

반응형
블로그 이미지

자연&사람

행복한 개발자 programmer since 1995.

,