[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. python多线程,限制线程数

    #encoding:utf8 import threading import time data = 0 def func(sleeptime): global data print threadin ...

  2. (Go)06. Printf格式化输出、Scanf格式化输入详解

    Print.Println .Printf .Sprintf .Fprintf都是fmt 包中的公共方法,在需要打印信息时需要用到这些函数,那么这些函数有什么区别呢? Print: 输出到控制台(不接 ...

  3. form内部的button_to不submit

    创建: 2017/09/12 更新: 2018/03/17 修正因为博客迁移造成的格式问题 官方文档 http://railsdoc.com/references/button_to 参考文档 htt ...

  4. js基础---字符串方法

    // var str1="xudandan"; // 字符串长度 var str1_length=str1.length;console.log(str1);console.log ...

  5. 【SQL】IN、EXISTS和表连接三者的效率比较

    一. IN和EXISTS比较 在许多基于基础表的查询中,为了满足一个条件,往往需要对另一个表进行查询.此时就会用到IN和EXISTS. 例如:查询departments表中存在的部门的人数. 1. 使 ...

  6. [Advanced Algorithm] - Inventory Update

    题目 依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新的数量. 返回当前的库存数组,且按货物 ...

  7. 【sqli-labs】 less40 GET -Blind based -String -Stacked(GET型基于盲注的堆叠查询字符型注入)

    提交,页面正常,说明是')闭合的 http://192.168.136.128/sqli-labs-master/Less-40/?id=1')%23 http://192.168.136.128/s ...

  8. 【sqli-labs】 less21 Cookie Injection- Error Based- complex - string ( 基于错误的复杂的字符型Cookie注入)

    这个和less20是一样的,唯一的不同在于添加了括号和使用了base64对cookie进行了编码(因为使用了base64_decode解码函数) admin被编码成了YWRtaW4=但是执行的SQL语 ...

  9. mvc登录授权特性

    public class CommonAuthorize : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContex ...

  10. 【转载】Servlet中的request与response

      一.HttpServletRequest概述   1.1.HttpServletRequest简介 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时, ...