2013-08-10 2 views
0

apple documentation에 설명 된 것처럼 디렉토리의 스냅 샷을 만들려고합니다.OS X : scandir() 함수의 dirent struct 속성에 문제가 있습니다.

scandir() 기능을 사용하고 싶습니다. 여기 문서에서입니다 :

scandir(const char *dirname, struct dirent ***namelist, int (*select)(const struct dirent *), 
    int (*compar)(const struct dirent **, const struct dirent **)); 

제대로 사용하는 방법을 모르겠다.

다음
-(void)createFolderSnapshotWithPath:(NSString *)pathString 
{ 

NSLog(@"snap"); 
const char *pathsToWatch=[pathString UTF8String]; 

struct dirent snapshot; 


scandir(pathsToWatch, &snapshot, NULL, NULL); // I have a warning here because 
               // &snapshot used wrong here 


NSLog(@"snap result: %llu | %s | %i",snapshot.d_ino, snapshot.d_name, snapshot.d_type); 
// snapshot.d_type returns 0 which means unknown type (DT_UNKNOWN) 

} 

dirent struct입니다 :

struct dirent { 
    ino_t d_ino;   /* file number of entry */ 
    __uint16_t d_reclen;  /* length of this record */ 
    __uint8_t d_type;  /* file type, see below */ 
    __uint8_t d_namlen;  /* length of string in d_name */ 
    char d_name[__DARWIN_MAXNAMLEN + 1]; /* name must be no longer than this */ 
}; 

내가 적절한 dirent struct 방법과 적절한 scandir() 기능에서 사용하는 방법을 만들 수 뜨거운 이해하지 못하고 여기 내 스냅 샷 기능을 구현하는 방법이다.

필자가이 기능에서 원하는 것은 나중에 다른 스냅 샷과 비교할 때 사용할 수있는 배열입니다.

답변

1

scandir()은 항목 배열을 할당합니다.

그래서 당신은 다음과 같이 2 있었던 파라미터를 선언해야합니다
struct dirent ** snapshot = NULL; 

그리고 성공적으로 후

scandir()이 같은 회원에 액세스 할 수 있습니다라고 한 : 예를 들어

printf("%s", snapshot[0]->d_name); 

.

의 항목과 함께 배열이 더 이상 사용하지 않을 경우, 모든 이상 반복하고 마지막

free(snapshot[i]); 
각 항목에 대한

와 전화 1 무료 항목은 수행

free(snapshot); 

이 모든 것이 함께 다음과 같이 보일 수 있습니다.

#include <dirent.h> 

int main(void) 
{ 
    struct dirent ** namelist = NULL; 
    int n = scandir(".", &namelist, NULL, alphasort); 
    if (n < 0) 
    { 
    perror("scandir"); 
    } 
    else 
    { 
    while (n--) 
    { 
     printf("%s\n", namelist[n]->d_name); 
     free(namelist[n]); 
    } 

    free(namelist); 
    } 
} 
+0

왜? 나는 그것을 이해하지 못한다. 그러나 지금 당장 시도 할 것이다. –

+0

@flinth : 배열은'struct dirent'에 대한 포인터 배열에 대한 포인터에 의해 접근된다. 이는 의도적으로 설계된 동작입니다. – alk

+0

안녕, 나 한테 설명해 줄 수있어, 어떻게'scandir()'을 사용하여 마지막으로 수정 한 날짜를 얻을 수 있을까? 가능한가? 애플의 문서가 나를 혼란스럽게 만든다. –