Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15974
  • 博文数量: 9
  • 博客积分: 411
  • 博客等级: 一等列兵
  • 技术积分: 120
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-13 22:50
文章分类

全部博文(9)

文章存档

2011年(2)

2010年(7)

我的朋友

分类: C/C++

2010-12-27 23:47:10

今天完成了一个能查找指定目录下的文件名和文件类型的程序。主要使用了glob函数。程序完成的任务:显示当前目录下的所有文件。

#include

#include

#include

 

#define PAT1 "./*"

#define PAT2 "./.*"

 

int

main()

{

    glob_t glob_result;

    int ret, i;

 

    //ret = glob(PAT1, GLOB_NOSORT|GLOB_NOCHECK, NULL, &glob_result);//GLOB_NOSORT falg使得结果不进行排序。默认会排序。

    ret = glob(PAT1, GLOB_NOCHECK, NULL, &glob_result);//

if (ret!=0) {

        fprintf(stderr, "Error.\n");

        exit(1);

    }

 

    ret = glob(PAT2, GLOB_NOSORT|GLOB_NOCHECK|GLOB_APPEND, NULL, &glob_result);//GLOB_APPEND flag实现了第二次条用glob函数时可以把结果追加到第一次条用时的后面。一般这个flag不会在第一次调用的时候使用。  

ret = glob(PAT2, GLOB_NOSORT|GLOB_NOCHECK, NULL, &glob_result);

    if (ret!=0) {

        fprintf(stderr, "Error.\n");

        exit(1);

    }

 

    for (i=0; i

        puts(glob_result.gl_pathv[i]);

    }

    printf("1 = %d\n", i);

    globfree(&glob_result);//切记此处不要忘记释放glob空间

 

 

    return 0;

}

 

 

 

阅读(752) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-01-03 16:14:49

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com