zoukankan      html  css  js  c++  java
  • C语言遍历文件和文件夹——————【Badboy】

    [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. ");

      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. ");

      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 ", 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 ", 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 ", visit_dirs, visit_files);

      }else{

      printf("Usage: listdir

    ");

      return;

      }

      return 0;

      }

  • 相关阅读:
    12864多级菜单实现,可方便实现无限级菜单(转)
    一起来学习PID
    STM32探秘 之FSMC
    RAD Studio (Delphi) Firemonkey 教程
    POS终端MAC算法-C语言实现
    STM32的PWM输入模式设置并用DMA接收数据
    AVR web server
    Linux 查看服务状态(服务与进程)
    linux设置开机自启动
    科目三靠边停车难度升级,超过50cm不合格怎么破?
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7099919.html
Copyright © 2011-2022 走看看