递归搜索目录,编译错误,我不明白

时间:2017-04-11 15:28:05

标签: c file search recursion

我需要递归搜索UNC路径以获取给定的文件名(request),我已成功连接到路径,并找到了搜索here的答案,但是,当我编译我的程序我收到以下错误:

find-util.c: In function ‘char* search_utils(const char*, int, bool)’:
find-util.c:51:16: error: ‘struct dirent’ has no member named ‘d_type’
       if (ent->d_type == DT_DIR)
                ^
find-util.c:51:26: error: ‘DT_DIR’ was not declared in this scope
       if (ent->d_type == DT_DIR)
                          ^

我在Ubuntu 16.04上使用mingw编译器进行交叉编译以便能够在Windows 7上运行。我错过了一个库吗?或者还有其他事情我还没有看到?

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <dirent.h>
#include <string.h>

static char *search_utils(const char *request, int depth, bool verbose)
{
    DIR *dir;
    struct dirent *ent;

    puts("Connecting to mgtutils..");

    if ((dir = opendir(MG_PATH)) != NULL)
    {
        while ((ent = readdir(dir)) != NULL)
        {
            if (verbose == true)
            {
                printf("Searching%s for %s\n", ent->d_name, request);
            }
            if (ent->d_type == DT_DIR)
            {
                if ((strlen(MG_PATH) + strlen(ent->d_name) + 1) > PATH_MAX)
                {
                    puts("Path to long, cannot");
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我在在线linux手册页READDIR(3)中找到了这个可能的解释:

  

POSIX.1强制要求的dirent结构中的唯一字段   是d_name和d_ino。其他字段是非标准化的,而不是   出现在所有系统上;有关更多详细信息,请参阅下面的注释。

因此,请编辑您的问题并说明您正在使用的操作系统/平台和编译器。

您还可以查看dirent.h

我是在/usr/include/dirent.h包含sys/dirent.h的cygwin上做到的。在/usr/include/sys/dirent.h中,我发现了struct dirent的多种风格,是的,在这种情况下,存在d_type组件。 (所以你可能不在cygwin ......)

因此,如果struct dirent在您的案例中没有提供d_type,您还可以使用其他什么? stat() ......

怎么样?