c - 递归搜索一个目录,编译错误我看不懂

标签 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");
                }
            }
        }
    }
}

最佳答案

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

The only fields in the dirent structure that are mandated by POSIX.1 are d_name and d_ino. The other fields are unstandardized, and not present on all systems; see NOTES below for some further details.

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

您还可以查看 dirent.h

我在 cygwin 上执行此操作,其中 /usr/include/dirent.h 包含 sys/dirent.h。在 /usr/include/sys/dirent.h 中,我发现了多种类型的 struct dirent,是的,在本例中是一个 d_type 组件存在。 (所以你可能不在 cygwin 上......)

那么,如果 struct dirent 在您的情况下没有提供 d_type,您还可以使用什么? stat()怎么样...

关于c - 递归搜索一个目录,编译错误我看不懂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43350231/

相关文章:

c - C 中是否使用了 IEEE-754 表示法?

c++ - 二维 C 样式数组崩溃

C 编程比较 float

c - 如何知道我的程序在运行时实际使用的是哪个共享库?

java - Java 中使用 javax.xml 的错误文件描述符 IOException

c:从文件中读取

algorithm - 遗传算法中的交叉方法

search - Sitecore 7 : Sorting Lucene results by field

arrays - Perl:使用 while 将文件加载到哈希中

java - Java在数组中查找具有多个关键字的对象