在另一个目录中查找目录

时间:2013-04-05 02:53:54

标签: c directory

我需要在不同的目录中找到特定目录,但由于某种原因,我的代码在当前目录中找到目录,但是当我开始在父目录中搜索特定的命名目录时,它无法在那里找到它

#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;
}

2 个答案:

答案 0 :(得分:2)

您需要检查系统调用的返回状态,特别是stat()

发生的事情是您读取了..目录中的名称,但是当您调用stat()时,您正在./name而不是../name上这样做。< / p>

此代码应说明要点:

#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;
}

答案 1 :(得分:0)

我在我的电脑上测试了它(并创建了目录../dirtest),它似乎工作正常。您确定您的目录已创建并且您正在正确的位置进行搜索吗?