[cpp]

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #define MAX_PATH_LENGTH 512

  #define MAX_FILE_EXTENSION 9

  unsigned long visit_dirs = 0;

  unsigned long visit_files = 0;

  void listdir(char *path){

  DIR *ptr_dir;

  struct dirent *dir_entry;

  int i = 0;

  char *child_path;

  char *file_path;

  child_path = (char*)malloc(sizeof(char)*MAX_PATH_LENGTH);

  if(child_path == NULL){

  printf("allocate memory for path failed.\n");

  return;

  }

  memset(child_path, 0, sizeof(char)*MAX_PATH_LENGTH);

  file_path = (char*)malloc(sizeof(char)*MAX_PATH_LENGTH);

  if(file_path == NULL){

  printf("allocate memory for file path failed.\n");

  free(child_path);

  child_path = NULL;

  return;

  }

  memset(file_path, 0, sizeof(char)*MAX_PATH_LENGTH);

  ptr_dir = opendir(path);

  while((dir_entry = readdir(ptr_dir)) != NULL){

  if(dir_entry->d_type & DT_DIR){

  if(strcmp(dir_entry->d_name,".") == 0 ||

  strcmp(dir_entry->d_name,"..") == 0){

  continue;

  }

  sprintf(child_path, "%s/%s", path, dir_entry->d_name);

  printf("[DIR]%s\n", child_path);

  visit_dirs++;

  listdir(child_path);

  }

  if(dir_entry->d_type & DT_REG){

  sprintf(file_path, "%s/%s", path, dir_entry->d_name);

  printf("[FILE]%s\n", file_path);

  visit_files++;

  }

  }

  free(child_path);

  child_path = NULL;

  free(file_path);

  file_path = NULL;

  }

  int main(int argc, char* argv[]){

  if(argc == 2){

  listdir(argv[1]);

  printf("Total DIR: %ld, Total FILE: %ld\n", visit_dirs, visit_files);

  }else{

  printf("Usage: listdir

\n");

  return;

  }

  return 0;

  }

C语言遍历文件和文件夹——————【Badboy】的更多相关文章

  1. 转: KindEditor 图片空间文件增加删除文件、文件夹功能(ASP语言环境)

    KindEditor 图片上传功能中集成的图片空间文件管理插件可以对已上传图片进行管理,十分便捷,只是没有图片删除功能,仔细研读xieliang分享的经验后,自己动手改造了一下,顺便分享给有同样需求的 ...

  2. C#递归遍历指定目录下文件和文件夹

    #region 使用递归查询某路径中的文件结构 public static void CheckFilePath() { ReadFilePath(); } public static void Re ...

  3. Python:遍历一个目录下所有的文件及文件夹,然后计算每个文件的字符和line的小程序

    编写了一个遍历一个目录下所有的文件及文件夹,然后计算每个文件的字符和line的小程序,先把程序贴出来. #coding=utf-8 ''' Created on 2014年7月14日 @author: ...

  4. Python遍历文件个文件夹

    在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件.文件夹操作的方法.下面列出: os.path.abspath(path) #返回绝对路径 os.path.basename ...

  5. C++遍历目录和文件夹

    我们需要一个结构体和几个函数.这些函数和结构体在<io.h>的头文件中,结构体为struct _finddata_t ,函数为_findfirst._findnext和_fineclose ...

  6. Linux 用C语言判断文件和文件夹

    Linux 用C语言判断文件和文件夹 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #inc ...

  7. Python小代码_15_遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间

    遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间 import osimport datetime def print_tree(dir_path): for ...

  8. C++遍历文件及文件夹代码

    可以遍历目录包含的文件及文件夹 #include <string> #include <vector> #include <io.h> using std::vec ...

  9. matlab遍历文件夹下所有图片和遍历所有子文件夹下图片

    做图像处理实验,经常需要遍历当前文件下所有图片.matlab当然很早就考虑了这个问题,库函数dir就是完成这个工作的.函数返回的是一个存放所有目录下文件信息的结构体,通过遍历结构体就可以达到访问所有文 ...

  10. Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)

    OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname")  改变当前脚本工作目 ...

随机推荐

  1. Django day05 视图层之 (HttpRequest) \ (HttpResponse) \ (JsonResponse) 对象

    一:视图层之HttpRequest对象 # 前台Post传过来的数据,包装到POST字典中 # request.POST # 前台浏览器窗口里携带的数据,包装到GET字典中 # request.GET ...

  2. springboot创建项目

    Springboot作为轻量级快速开发受到无数java人的青睐,Spring Boot是为了简化Spring应用的创建.运行.调试.部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过 ...

  3. Integer应该用==还是equals

    问题引出:“Integer应该用==还是equals” 讨论这个问题之前我们先放一段代码 public static void main(String[] args) { Integer a1 = 2 ...

  4. python 5:str(某一变量)(将其他数字解释为字符串)

    age = messege = "Your's age is " + str(age) #将其他数字更改为字符串 print(messege) 运行结果应该是: Your's ag ...

  5. 小白写的一个ASP.NET分页控件,仅供娱乐

    无聊,第一次写博客,自己动手写了一个分页控件.由于我是新手,有很多地方写得不够好,希望各位大牛多多指正.哈哈哈 /// <summary> /// 分页控件 /// </summar ...

  6. CruiseControl.NET 的安装与配置

    CC.NET 是 CruiseControl.NET 的简称,官方网站在这里. 官方的定义是自动连续集成服务, 使用Microsoft .NET Framework实现. 另外它还有Java的版本叫做 ...

  7. 编码的来历和使用 utf-8 和GB2312比较

    经常我们打开外国网站的时候出现乱码,又或者打开很多非英语的外国网站的时候,显示的都是口口口口口的字符, wordpress程序是用的UTF-8,很多cms用的是GB2312. ● 为什么有这么多编码? ...

  8. openMSP430之io_test

    openMSP430: IO functionality test with interupt #include "omsp_system.h" volatile char shi ...

  9. Caffe2:使用Caffe构建LSTM网络

    前言: 一般所称的LSTM网络全叫全了应该是使用LSTM单元的RNN网络. 原文:(Caffe)LSTM层分析 入门篇:理解LSTM网络 LSTM的官方简介: http://deeplearning. ...

  10. socket主要函数介绍

    1.   基本套接字函数(1)socket函数原型 socket(建立一个socket文件描述符) 所需头文件 #include <sys/types.h> #include <sy ...