dirent.h和dirent.c下载

从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

 

`dirent.h` 是 C 语言中的一个头文件提供了一些函数和结构体,用于处理目录和文件操作。下面是对 `dirent.h` 中常用的一些函数和结构体进行详细介绍: 1. 结构体 `dirent`: `struct dirent` 是用来表示目录条目的结构体,其中包含了以下成员: - `ino_t d_ino`:目录中的 inode 编号。 - `off_t d_off`:目录项在目录文件中的偏移量。 - `unsigned short d_reclen`:目录项长度。 - `unsigned char d_type`:目录项类型。 - `char d_name[]`:目录项名称。 2. 函数 `DIR *opendir(const char *dirname)`: 该函数用于打开一个目录,并返回一个指向 `DIR` 类型的指针,该指针用于后续的目录操作。`dirname` 参数是要打开的目录名。 3. 函数 `struct dirent *readdir(DIR *dirp)`: 该函数用于读取一个目录中的下一个条目。`dirp` 参数是之前通过 `opendir` 打开的目录指针。返回一个指向 `struct dirent` 结构体的指针,或者在到达目录尾部或发生错误时返回 `NULL`。 4. 函数 `int closedir(DIR *dirp)`: 该函数用于关闭先前通过 `opendir` 打开的目录。`dirp` 参数是目录指针。成功关闭返回 0,失败返回 -1。 5. 函数 `void rewinddir(DIR *dirp)`: 该函数将目录流的位置重置为目录的开头。 6. 函数 `int scandir(const char *dir, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **))`: 该函数用于获取一个目录下的文件列表,并以动态分配的方式将结果存储在 `namelist` 中。`dir` 参数是要扫描的目录名,`filter` 和 `compar` 是可选的过滤和排序函数。 以上是 `dirent.h` 中一些常用的函数和结构体。使用这些函数和结构体,可以方便地进行目录和文件的遍历、读取和操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值