2013-04-05 4 views
0

다른 디렉토리에서 특정 디렉토리를 찾아야하지만, 내 코드가 현재 디렉토리에서 디렉토리를 찾지 만 상위 디렉토리에서 특정 이름이 지정된 디렉토리를 검색하기 시작하면 찾지 못합니다 거기다른 디렉토리에서 디렉토리 찾기

#include <dirent.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <sys/stat.h> 
#include <sys/types.h> 
#include <unistd.h> 
#include <string.h> 
void print_dir(char *dir_n, char *file) 
{ 
DIR *dir = opendir(dir_n); 

struct dirent *Dirent; 
struct stat stats; 

while(1) 
{ 
    Dirent = readdir(dir); 
    if (Dirent == NULL) 
    { 
     break; 
    } 

     stat(Dirent->d_name, &stats); 
     if (S_ISDIR(stats.st_mode)) 
     { 
      if(strcmp(file ,Dirent->d_name) == 0 && S_ISDIR(stats.st_mode)) 
      { 
       printf("found\n"); 
       break; 

      } 
     } 
} 
closedir(dir); 
} 
int main(int argc, const char * argv[]) 
{ 
    print_dir("..", "dirtest"); 
    return 0; 
} 
+0

"파일"은 디렉토리입니다. 'dirent' 구조체는'DT_DIR' 멤버 인'd_type' 멤버를 가지고 있습니다. –

+0

'print_dir' 호출에서'..' 대신'../'을 시도 했습니까? 이게 효과가 있는지 모르겠지만, 나는 단지 호기심이 많습니다. – maditya

+0

@maditya no ../ 작동하지 않습니다. – swiftk

답변

2

시스템 호출의 반환 상태, 특히 stat()을 확인해야합니다. 무슨 일

당신이 .. 디렉토리에 이름을 읽을 수 있지만 당신이 stat()를 호출 할 때, 당신은 ./name하지 ../name에 그렇게되어 있다는 점이다.

이 코드는 점 입증해야

#include <dirent.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sys/stat.h> 
#include <unistd.h> 
#include <errno.h> 

void print_dir(char *dir_n, char *file) 
{ 
    DIR *dir = opendir(dir_n); 

    if (dir == 0) 
    { 
     int errnum = errno; 
     fprintf(stderr, "error: opendir(\"%s\") failed (%d: %s)\n", dir_n, errnum, strerror(errnum)); 
     exit(1); 
    } 

    struct dirent *Dirent; 

    while ((Dirent = readdir(dir)) != 0) 
    { 
     struct stat stats; 
     if (stat(Dirent->d_name, &stats) < 0) 
     { 
      int errnum = errno; 
      fprintf(stderr, "error: failed to stat(\"%s\") (%d: %s)\n", Dirent->d_name, errnum, strerror(errnum)); 
     } 
     else if (S_ISDIR(stats.st_mode)) 
     { 
      if (strcmp(file, Dirent->d_name) == 0) 
      { 
       printf("found directory %s (inode = %ld)\n", Dirent->d_name, (long)stats.st_ino); 
       break; 
      } 
      else 
       printf("found directory %s - not a match for %s\n", Dirent->d_name, file); 
     } 
     else 
     { 
      printf("%s is not a directory\n", Dirent->d_name); 
     } 
    } 
    closedir(dir); 
} 

int main(void) 
{ 
    print_dir("..", "dirtest"); 
    return 0; 
} 

을 그리고 존재하는 경우이 사소한 변형 디렉토리를 ../dirtest을 찾아야한다 : 당신은 경우 얻을 전화 stat``필요하지 않습니다

#include <dirent.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sys/stat.h> 
#include <unistd.h> 
#include <errno.h> 

void print_dir(char *dir_n, char *file) 
{ 
    DIR *dir = opendir(dir_n); 

    if (dir == 0) 
    { 
     int errnum = errno; 
     fprintf(stderr, "error: opendir(\"%s\") failed (%d: %s)\n", dir_n, errnum, strerror(errnum)); 
     exit(1); 
    } 

    struct dirent *Dirent; 

    while ((Dirent = readdir(dir)) != 0) 
    { 
     char fullname[1024]; 
     snprintf(fullname, sizeof(fullname), "%s/%s", dir_n, Dirent->d_name); 
     struct stat stats; 
     if (stat(fullname, &stats) < 0) 
     { 
      int errnum = errno; 
      fprintf(stderr, "error: failed to stat(\"%s\") (%d: %s)\n", fullname, errnum, strerror(errnum)); 
     } 
     else if (S_ISDIR(stats.st_mode)) 
     { 
      if (strcmp(file, Dirent->d_name) == 0) 
      { 
       printf("found directory %s (%s) (inode = %ld)\n", Dirent->d_name, fullname, (long)stats.st_ino); 
       break; 
      } 
      else 
       printf("found directory %s - not a match for %s\n", fullname, file); 
     } 
     else 
     { 
      printf("%s is not a directory\n", fullname); 
     } 
    } 
    closedir(dir); 
} 

int main(void) 
{ 
    print_dir("..", "dirtest"); 
    return 0; 
} 
+0

감사합니다. – swiftk

0

내가 내 컴퓨터에서 (그리고 그것을 위해 디렉토리를 만들었습니다 ../dirtest), 그리고 그것을 잘 작동하는 것 같습니다. 디렉토리가 만들어지고 올바른 장소에서 검색하고 있습니까?

+0

예 여러 디렉토리가 있고 다른 디렉토리는 하나만 찾았지만 올바른 장소에서 검색하고 있습니다 – swiftk

+0

디렉토리의 이름은 무엇입니까? – maditya

+0

그냥 그 기능을 테스트하기 위해 만든 디렉토리 – swiftk

관련 문제