Chinaunix首页 | 论坛 | 博客
  • 博客访问: 348619
  • 博文数量: 161
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 345
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-13 11:04
文章分类

全部博文(161)

文章存档

2015年(15)

2014年(144)

2013年(2)

我的朋友

分类: C/C++

2014-10-30 13:15:39


点击(此处)折叠或打开

  1. /*
  2.  ============================================================================
  3.  Name : scandir.c
  4.  Author :
  5.  Version :
  6.  Copyright : Your copyright notice
  7.  Description : Hello World in C, Ansi-style
  8.  ============================================================================
  9.  */

  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <dirent.h>
  13. #include <ftw.h>
  14. #include <unistd.h>
  15. #include <stdlib.h>
  16. int SelectFlies(const struct dirent *pDir) {
  17.     return 1;
  18.     int iPrefixLen = 0;
  19.     int iSelectResult = 0;
  20.     if (pDir == NULL) {
  21.         printf("SelectFlies():input parameter is NULL!");
  22.         return 0;
  23.     }
  24.     // 匹配文件前缀
  25.     iPrefixLen = strlen("test"); // 前缀为 "Test_"
  26.     iSelectResult = (0 == strncmp(pDir->d_name, "test", iPrefixLen));
  27.     if (iSelectResult == 1) // 找到了匹配前缀的文件
  28.     {
  29.         return 1;
  30.     } else {
  31.         return 0;
  32.     }
  33. }

  34. int main(void) {
  35.     int szDirectory[256] = { 0 };
  36.     int iRetValue = 0;
  37.     struct dirent**namelist = NULL;
  38.     int iLoopFlag = 0;
  39.     memcpy(szDirectory, "./", strlen("./"));
  40.     while (1) {
  41.         fprintf(stdout, "\33[J");
  42.         // 用 scandir 函数来选择文件 , 选出来的文件按照字母表顺序排列
  43.         iRetValue = scandir((char *) szDirectory, &namelist, SelectFlies, alphasort);
  44.         // 返回负值 , 是因为没有扫描目录 , 或扫描出错
  45.         if (iRetValue < 0) {
  46.             printf("Execscandir failed, please check!");
  47.             return -1;
  48.         }
  49.         printf("The amount of scanded fileis:%d\n", iRetValue);
  50.         // 将扫描到的文件打印出来
  51.         for (iLoopFlag = 0; iLoopFlag < iRetValue; iLoopFlag++) {
  52.             printf("Thescanded file %d is:%s\n", iLoopFlag + 1, namelist[iLoopFlag]->d_name);
  53.             free(namelist[iLoopFlag]);
  54.             fflush(0);
  55.         }

  56.         fprintf(stdout, "\33[1;1H");
  57.         free(namelist);
  58.         sleep(2);

  59.     }
  60.     return 0;
  61. }

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