2013-04-05 118 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

你不需要'stat'打电话得到,如果“文件”是一个目录。 'dirent'结构有一个'd_type'成员,它是目录的'DT_DIR'。 – 2013-04-05 02:57:31

+0

您在'print_dir'调用中是否尝试过'../'而不是'..'?不知道这是否有效,但我只是好奇。 – maditya 2013-04-05 02:58:35

+0

@maditya no ../将不起作用 – swiftk 2013-04-05 03:10:52

回答

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如果它存在:

#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 2013-04-05 13:51:57

0

我测试了它在我的电脑(和创建的目录../dirtest的),它似乎工作正常。你确定你的目录是否已经创建,并且你在正确的地方搜索?

+0

是的,我有几个目录,它找到一个,但没有找到其他人,我在正确的地方搜索 – swiftk 2013-04-05 03:06:50

+0

它找到的目录的名称是什么? – maditya 2013-04-05 03:11:55

+0

它只是我创建的目录来测试该功能 – swiftk 2013-04-05 03:13:37