在C中使用Glob()列出目录中的文件 [英] List files in directories using Glob() in C

查看:61
本文介绍了在C中使用Glob()列出目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,到目前为止,我有以下代码:

  #include< glob.h>#include< string.h>#include< stdio.h>#定义错误1#定义失败-1int main(int ac,char ** av){glob_t globlist;我i = 0;如果(ac == 1)返回(-1);别的{if(glob(av [1],GLOB_PERIOD,NULL,& globlist)== GLOB_NOSPACE||glob(av [1],GLOB_PERIOD,NULL和&globlist)== GLOB_NOMATCH)退货(失败);如果(glob(av [1],GLOB_PERIOD,NULL和&globlist)== GLOB_ABORTED)返回(错误);同时(globlist.gl_pathv [i]){printf(%s \ n",globlist.gl_pathv [i]);i ++;}}返回(0);} 

例如,当我键入 ./a.out"*" 时,它会打印我所在的所有文件以及目录,但不会打印目录中的内容.我应该如何打印所有文件,包括子文件/文件夹?

谢谢

解决方案

使用 nftw() 而不是 glob() 如果您要检查整棵树,而不是一个特定的路径和文件名模式.

(使用opendir()/readdir()/closedir()来重制轮子绝对是愚蠢的,尤其是因为nftw()应该优雅地处理文件系统更改,而自旋树遍历代码通常会忽略所有内容坚硬的东西,并且只能在您自己的机器上以最佳条件工作,而在其他地方则无法通过壮观而奇妙的方式失败.)

在过滤器功能中,使用 fnmatch() 来确定是否使用glob模式接受文件名.

如果您希望使用正则表达式进行过滤,请使用 regcomp() 来编译模式,然后再调用nftw(),然后全局模式上的Wikipedia文章.和正则表达式非常有用且内容丰富.

以上所有内容均在POSIX.1-2008中定义,因此它们可以在所有POSIX-y操作系统中移植.

Basically, so far I have this code:

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

# define ERROR 1
# define FAILURE -1

int main(int ac, char **av)
{
  glob_t globlist;
  int i;

  i = 0;               
  if (ac == 1)
    return (-1);
  else
    {
      if (glob(av[1], GLOB_PERIOD, NULL, &globlist) == GLOB_NOSPACE
          || glob(av[1], GLOB_PERIOD, NULL, &globlist) == GLOB_NOMATCH)
        return (FAILURE);
      if (glob(av[1], GLOB_PERIOD, NULL, &globlist) == GLOB_ABORTED)
        return (ERROR);
      while (globlist.gl_pathv[i])
        {
          printf("%s\n", globlist.gl_pathv[i]);
          i++;
        }
    }
  return (0);
}

When I type ./a.out "*" for example it prints all my files where I am, aswell as directories, but it doesn't print what is inside directories. How should I do to print ALL files, including sub-files/folders?

Thanks

解决方案

Use nftw() instead of glob() if you want to examine entire trees, rather than one specific path and filename pattern.

(It is absolutely silly to reinvent the wheel by going at it using opendir()/readdir()/closedir(), especially because nftw() should handle filesystem changes gracefully, whereas self-spun tree walking code usually ignores all the hard stuff, and only works in optimal conditions on your own machine, failing in spectacular and wonderful ways elsewhere.)

In the filter function, use fnmatch() to decide whether the file name is acceptable using glob patterns.

If you wish to filter using regular expressions instead, use regcomp() to compile the pattern(s) before calling nftw(), then regexec() in your filter function. (Regular expressions are more powerful than glob patterns, and they are compiled to a tight state machine, so they are quite efficient, too.)

If you are unsure about the difference, the Wikipedia articles on glob patterns and regular expressions are very useful and informative.

All of the above are defined in POSIX.1-2008, so they are portable across all POSIX-y operating systems.

这篇关于在C中使用Glob()列出目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆