dirent.h和dirent.c下载

本文介绍了一种在Win32环境下实现POSIX目录遍历功能的方法,通过自定义direct.h和direct.cpp文件,实现了opendir、closedir、readdir和rewinddir等函数,解决了在Windows平台下使用POSIX目录操作接口的问题。

从github down了一个工程,MS编译报找不到direct.h,网上搜索,借鉴https://blog.csdn.net/yapingxin/article/details/51444133http://www.two-sdg.demon.co.uk/curbralan/code/dirent/dirent.html,下载对应代码,添加到工程中,将问题解决。下面贴出.c和.h。

direct.cpp

/*

    Implementation of POSIX directory browsing functions and types for Win32.

    Author:  Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
    History: Created March 1997. Updated June 2003 and July 2012.
    Rights:  See end of file.

*/

#include <dirent.h>
#include <errno.h>
#include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
extern "C"
{
#endif

typedef ptrdiff_t handle_type; /* C99's intptr_t not sufficiently portable */

struct DIR
{
    handle_type         handle; /* -1 for failed rewind */
    struct _finddata_t  info;
    struct dirent       result; /* d_name null iff first time */
    char                *name;  /* null-terminated char string */
};

DIR *opendir(const char *name)
{
    DIR *dir = 0;

    if(name && name[0])
    {
        size_t base_length = strlen(name);
        const char *all = /* search pattern must end with suitable wildcard */
            strchr("/\\", name[base_length - 1]) ? "*" : "/*";

        if((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
           (dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0)
        {
            strcat(strcpy(dir->name, name), all);

            if((dir->handle =
                (handle_type) _findfirst(dir->name, &dir->info)) != -1)
            {
                dir->result.d_name = 0;
            }
            else /* rollback */
            {
                free(dir->name);
                free(dir);
                dir = 0;
            }
        }
        else /* rollback */
        {
            free(dir);
            dir   = 0;
            errno = ENOMEM;
        }
    }
    else
    {
        errno = EINVAL;
    }

    return dir;
}

int closedir(DIR *dir)
{
    int result = -1;

    if(dir)
    {
        if(dir->handle != -1)
        {
            result = _findclose(dir->handle);
        }

        free(dir->name);
        free(dir);
    }

    if(result == -1) /* map all errors to EBADF */
    {
        errno = EBADF;
    }

    return result;
}

struct dirent *readdir(DIR *dir)
{
    struct dirent *result = 0;

    if(dir && dir->handle != -1)
    {
        if(!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
        {
            result         = &dir->result;
            result->d_name = dir->info.name;
        }
    }
    else
    {
        errno = EBADF;
    }

    return result;
}

void rewinddir(DIR *dir)
{
    if(dir && dir->handle != -1)
    {
        _findclose(dir->handle);
        dir->handle = (handle_type) _findfirst(dir->name, &dir->info);
        dir->result.d_name = 0;
    }
    else
    {
        errno = EBADF;
    }
}

#ifdef __cplusplus
}
#endif

/*

    Copyright Kevlin Henney, 1997, 2003, 2012. All rights reserved.

    Permission to use, copy, modify, and distribute this software and its
    documentation for any purpose is hereby granted without fee, provided
    that this copyright and permissions notice appear in all copies and
    derivatives.
    
    This software is supplied "as is" without express or implied warranty.

    But that said, if there are any problems please get in touch.

*/

direct.h

#ifndef DIRENT_INCLUDED
#define DIRENT_INCLUDED

/*

    Declaration of POSIX directory browsing functions and types for Win32.

    Author:  Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
    History: Created March 1997. Updated June 2003.
    Rights:  See end of file.
    
*/

#ifdef __cplusplus
extern "C"
{
#endif

typedef struct DIR DIR;

struct dirent
{
    char *d_name;
};

DIR           *opendir(const char *);
int           closedir(DIR *);
struct dirent *readdir(DIR *);
void          rewinddir(DIR *);

/*

    Copyright Kevlin Henney, 1997, 2003. All rights reserved.

    Permission to use, copy, modify, and distribute this software and its
    documentation for any purpose is hereby granted without fee, provided
    that this copyright and permissions notice appear in all copies and
    derivatives.
    
    This software is supplied "as is" without express or implied warranty.

    But that said, if there are any problems please get in touch.

*/

#ifdef __cplusplus
}
#endif

#endif

 

打开下面链接,直接免费下载资源: https://renmaiwang.cn/s/mnwhx 遇到VS工程报错提示“无法打开包括文件: dirent.h”,建议将目标文件复制至工程目录中,并直接复制到(VS 库目录)中,例如C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include。 dirent.h是C语言中用于获取目录内容的一个头文件,它是POSIX标准的一部分,用于定义目录项相关的函数数据结构。这个头文件提供了用于读取目录流的函数原型,如opendir、readdir以及closedir等,这些函数能帮助用户以流的形式逐个获取目录中的文件信息。在UNIXLinux系统中,这个头文件是标准系统调用的一部分,而在Windows系统中,由于其文件系统与UNIX有较大差异,通常不会直接使用dirent.h。 在Windows环境下,尤其是使用Visual Studio这类的集成开发环境进行项目开发时,如果遇到“无法打开包括文件: dirent.h”的错误,通常意味着需要将这个头文件的源代码或者预编译的库文件引入到项目中,以便程序能够正确编译链接。按照提示,开发者可以将dirent.h的源码文件复制到工程目录里,但更为常见直接的方法是将其复制到Visual Studio的库目录中,如C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include,这样在编译时就能识别到该头文件,避免编译错误。 需要注意的是,直接复制到库目录中的dirent.h应当是适用于Windows环境的版本,或者是一个能够适配Windows API的兼容版本。在实际的开发实践中,如果是在Windows环境下,开发者可能需要使用Windows API提供的功能,如Find
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值