目录函数glob、opendir、readdir、closedir

分析、读取目录
有两种方式
一、glob
二、opendir readdir closedir rewenddir fseek seekdir talldir

glob

#include <glob.h>
int glob(const char *pattern, int flags,
int (*errfunc) (const char *epath, int eerrno),
glob_t *pglob);
void globfree(glob_t *pglob);

参数:

const char *pattern

: 通配符、模式

int flags
The argument flags is made up of the bitwise OR of zero or more the following symbolic constants, which modify the behavior of glob():
使用按位或的形式把0个或多个的符号信息或进flags下面
       GLOB_ERR
              Return upon a read error (because a directory does not have read permission, for example).  By default, glob() attempts carry on despite errors, reading all of the directories that it can.

       GLOB_MARK
              Append a slash to each path which corresponds to a directory.

       GLOB_NOSORT
              Don't sort the returned pathnames.  The only reason to do this is to save processing time.  By default, the returned pathnames are sorted.
返回的路径不排序
       GLOB_DOOFFS
              Reserve pglob->gl_offs slots at the beginning of the list of strings in pglob->pathv.  The reserved slots contain null pointers.

       GLOB_NOCHECK
              If no pattern matches, return the original pattern.  By default, glob() returns GLOB_NOMATCH if there are no matches.
没有模式能匹配的,那么就把当前的pattern本身返回
       GLOB_APPEND
              Append the results of this call to the vector of results returned by a previous call to glob().  Do not set this flag on the first invocation of glob().
追加,往指定的结果追加
       GLOB_NOESCAPE
              Don't allow backslash ('\') to be used as an escape character.  Normally, a backslash can be used to quote the following character, providing a mechanism to  turn  off  the special meaning metacharacters.
int (*errfunc) (const char *epath, int eerrno):

函数,返回值int型,保存当前出去的路径和出去的原因。
在解析第一个参数pattern时,会出现各种各样的问题,比如没有权限解析或者路径不存在
作用:在这里放个函数的入口地址,如果出错,会把解析出错的路径和错误原因回填在这里函数里

glob_t *pglob

解析第一个参数pattern的结果放到glob_t 类型所指向的地址中

返回值

RETURN VALUE
       On successful completion, glob() returns zero.  Other possible returns are:

       GLOB_NOSPACE
              for running out of memory,

       GLOB_ABORTED
              for a read error, and

       GLOB_NOMATCH
              for no found matches.

typedef struct {
               size_t   gl_pathc;    /* Count of paths matched so far  */
				计算能和pattern相匹配的名字有多少个,类似于int argc
               char   **gl_pathv;    /* List of matched pathnames.  */
               和pattern相匹配的名字,类似于char **argv
               size_t   gl_offs;     /* Slots to reserve in gl_pathv.  */
           } glob_t;

函数使用

/*************************************************************************
	> File Name: glob.c
	> Author: erfenjiao 
	> Mail: 630166475@qq.com 
	> Created Time: 2021年06月20日 星期日 21时47分58秒
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<glob.h>

#define PAT "/etc/.*"

// int errfunc(const char * errpath , int errno){
//     puts(errpath);
//     fprintf(stderr , "ERROR MSG :%s\n",strerror(errno));
//     return 0;
// }被注释掉的内容为错误处理函数
int main(int argc , char ** argv){

    glob_t globres;
    //int err;

    glob(PAT , 0 , NULL , &globres);
    // if(err)
    // {
    //     printf("Error code = %d\n",err);
    //     exit(1);
    // }

    for(int i = 0 ; i < globres.gl_pathc ; i++)
    {
        puts(globres.gl_pathv[i]);
    }
    globfree(&globres); //释放glob函数第四个参数所开拓的空间

    exit(0);
}

结果

/etc/.
/etc/..
/etc/.pwd.lock

opendir readdir closedir

opendir()

#include <sys/types.h>
#include <dirent.h>
DIR * opendir(const char *name);

readdir()

#include <dirent.h>
struct dirent *readdir(DIR *dirp);

closedir()

#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);

函数使用

/*************************************************************************
	 File Name: readdir.c
	 Author: erfenjiao 
	 Mail: 630166475@qq.com 
	 Created Time: 2021年07月22日 星期四 09时09分42秒
 ************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>

#define PAT "/etc/"

int main()
{
    DIR * dp;
    struct dirent * cur;

    dp = opendir(PAT);
    if(dp == NULL)
    {
        perror("opendir");
        exit(1);
    }
    while((cur = readdir(dp)) != NULL)
    {
        puts(cur->d_name);
    }
    closedir(dp);
    exit(0);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值