Chinaunix首页 | 论坛 | 博客
  • 博客访问: 73051
  • 博文数量: 15
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 57
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-30 09:39
个人简介

程序改变世界!

文章分类

全部博文(15)

文章存档

2018年(1)

2017年(4)

2016年(7)

2015年(3)

我的朋友

分类: C/C++

2017-06-22 22:46:19

需求:
   使用回调函数遍历指定目录。

实现:
    函数实现处,橙色部分为重点,即路径变量控制:
 点击(
此处)折叠或打开
  1. /********************************************************
  2. *filename    : scan.c
  3. *descriptor    : source file - scan the directory that given
  4. *autor        : Rocky
  5. *date        : 2017-06-22
  6. ********************************************************/

  7. #include "scan.h"

  8. #ifdef _MAN_READDIR
  9. //On Linux, the dirent structure is defined as follows:

  10. struct dirent {
  11.     ino_t d_ino; /* inode number */
  12.     off_t d_off; /* not an offset; see NOTES */
  13.     unsigned short d_reclen; /* length of this record */
  14.     unsigned char d_type; /* type of file; not supported
  15.                                  by all filesystem types */
  16.     char d_name[256]; /* filename */    
  17. };
  18. /*
  19. d_type:

  20. DT_BLK This is a block device.
  21. DT_CHR This is a character device.
  22. DT_DIR This is a directory.
  23. DT_FIFO This is a named pipe (FIFO).
  24. DT_LNK This is a symbolic link.
  25. DT_REG This is a regular file.
  26. DT_SOCK This is a UNIX domain socket.
  27. DT_UNKNOWN The file type is unknown.
  28. */
  29. #endif

  30. int start_scan(const char *dirname)
  31. {
  32.     DIR *dir_ptr;
  33.     struct dirent *ptr;
  34.     char deeper_dir[PATH_MAX] = {0};

  35.     dir_ptr = opendir(dirname);
  36.     if (NULL == dir_ptr)
  37.     {
  38.         perror("opendir error");
  39.         return -1;
  40.     }
  41.     
  42.     while ((ptr = readdir(dir_ptr)))
  43.     {
  44.         /* ignore the "." nad ".." */
  45.         if (!strcmp(ptr->d_name, ".") || !(strcmp(ptr->d_name, "..")))
  46.             continue;

  47.         printf("name is %s\n", ptr->d_name);
  48.         if (DT_REG == ptr->d_type)
  49.         {
  50.             printf("+++++ I am a regule file\n");
  51.         }
  52.         else if (DT_DIR == ptr->d_type)
  53.         {
  54.             printf("----- I am a directory\n");

  55.             strcpy(deeper_dir, dirname);    
  56.             strcat(deeper_dir, "/");
  57.             strcat(deeper_dir, ptr->d_name);
  58.             
  59.             start_scan(deeper_dir);
  60.         }
  61.     }

  62.     closedir(dir_ptr);

  63.     return 0;
  64. }
头文件定义:

点击(此处)折叠或打开

  1. /********************************************************
  2. *filename    : scan.h
  3. *descriptor    : head file - scan the directory that given
  4. *autor        : Rocky
  5. *date        : 2017-06-22
  6. ********************************************************/

  7. #ifndef _SCAN_H_
  8. #define _SCAN_H_

  9. #include <stdio.h>
  10. #include <dirent.h>
  11. #include <string.h>
  12. #include <sys/types.h>

  13. #ifndef PATH_MAX
  14.     #define PATH_MAX 256
  15. #endif //PATH_MAX

  16. int start_scan(const char *dirname);

  17. #endif // _SCAN_H_

点击(此处)折叠或打开

  1. /**********************************************************
  2. *filename    : main.c
  3. *autor        : Rocky
  4. *date        : 2017-06-22
  5. ************************************************************/

  6. #include "scan.h"

  7. int main(int argc, char *argv[])
  8. {
  9.     if (argc < 2)
  10.     {
  11.         printf("Usage : %s dirname\n", argv[0]);
  12.         return -1;
  13.     }

  14.     printf("dirname is %s\n", argv[1]);
  15.     start_scan(argv[1]);

  16.     return 0;
  17. }
工程管理(简单管理,日后还需深入学习makefile,以便进行高级的项目管理):

点击(此处)折叠或打开

  1. CC=gcc
  2. target=main
  3. CSOURCE:=
  4. CSOURCE+=main.c
  5. CSOURCE+=scan.c
  6. CFLAGS:=
  7. CFLAGS+=-Wall
  8. $(target):$(CSOURCE)
  9. $(CC) -o $@ $^ $(CFLAGS)
  10. clean:
  11. rm $(target)
运行结果:
    

总结:
     实现了需要的功能。因为平时逻辑思维能力较差(脑子转不过来弯),所以在工作中遇到这类问题时往往反应迟钝,耽误大量宝贵时间。日后多加强这方面的练习,多做这种小练习,让大脑转动起来。
 

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