[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. PCB MS SQL 将字符串分割,并指定索引返回字符串(标量函数)

    Create FUNCTION [dbo].[SplitIndex] ( @str AS VARCHAR(max), @Index AS INT, ) = '/' ) ) AS BEGIN ) --待 ...

  2. Labeling Balls(拓扑)

    http://poj.org/problem?id=3687 看题意看了半天没看懂怎么回事,看完Discuss彻底凌乱了..后来看了题解才懂,就是逆向建图+拓扑排序,建图时要判重边. #include ...

  3. protobuf 编译 java js文件详解

    首先下载protobuf.exe 下载地址:https://download.csdn.net/download/qq_34756156/10220137 MessageBody.proto synt ...

  4. SyntaxError: EOL while scanning string literal的解决

    2281 python中字符串的最后一个字符是斜杠会导致出错:SyntaxError: EOL while scanning string literal [背景] Python 2.7.2 中想要通 ...

  5. C - Crazy Town

    Problem description Crazy Town is a plane on which there are n infinite line roads. Each road is def ...

  6. HTML <!DOCTYPE>标签

    一般一个基本html页面的结构,如下代码所示: <html> <head> <title>我是基本的页面结构</title> </head> ...

  7. Spring Boot (18) @Async异步

    通常我们在某网站发送邮件验证码时,首先会提示验证码已发送,然而此时可能没有收到验证码,过几秒种才真正的收到.如果是同步会先验证发送是否成功然后再通知,如果是异步可以先通知用户已发送,并释放请求,然后再 ...

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

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

  9. Python语言之常用函数

    1.input(),raw_input() input() = eval( raw_input() ) 其中raw_input()将所有的输入当做字符串处理. eval(str [,globals [ ...

  10. VC维与DNN的Boundary

    原文链接:解读机器学习基础概念:VC维来去 作者:vincentyao 目录: 说说历史 Hoeffding不等式 Connection to Learning 学习可行的两个核心条件 Effecti ...