首页 > 其他分享 >递归查找目录下的所有txt文件

递归查找目录下的所有txt文件

时间:2023-10-10 14:34:51浏览次数:28  
标签:txt name 递归 int st char 查找 include ptr

#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int getTxtNum(const char* path) {
  // 打开目录
  DIR* dir = opendir(path);
  if (dir == NULL) {
    perror("opendir");
    return 0;
  }
  
  struct dirent* ptr = NULL;
  int count = 0;
  struct stat st;
  while ((ptr = readdir(dir)) != NULL) {
    // 遍历到 . 和 .. 项跳过
    if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
      continue;
    }

    char fullPath[1024];
    // 注意这里必须使用完整路径!不能直接使用ptr->d_name,否则会找不到文件
    snprintf(fullPath, sizeof(fullPath), "%s/%s", path, ptr->d_name);
    int flag = stat(fullPath, &st);
    if (flag) {
      printf("%s\n", fullPath);
      perror("read error");
      return 0;
    }
    // 读取到一个目录项
    if (S_ISDIR(st.st_mode)) {
      char newPath[1024];
      snprintf(newPath, sizeof(newPath), "%s/%s", path, ptr->d_name);
      count += getTxtNum(newPath);
    } else if (S_ISREG(st.st_mode)) {  // 普通文件
      char* p = strstr(ptr->d_name, ".txt");
      if(p != NULL && *(p+4) == '\0') {
        count++;
        printf("%s/%s\n", path, ptr->d_name);
      }
    }
  }
  closedir(dir);
  return count;
}
int main(int argc, char* argv[]) {
  if (argc != 2) {
    fprintf(stderr, "please input a directory!\n");
    return -1;
  }

  int num = getTxtNum(argv[1]);
  printf("the number of txt is %d\n", num);

  return 0;
}

标签:txt,name,递归,int,st,char,查找,include,ptr
From: https://www.cnblogs.com/hacker-dvd/p/17754579.html

相关文章

  • MySQL存储过程、递归调用
    MySQL存储过程、递归调用实现字典数据的预处理,维护类别表、数据表、tree表,数据库在jwzh_manager库1、先将excel导入到system_dict表,按表字段注释对应匹配。2、编写存储过程处理数据CREATEDEFINER=`root`@`%`PROCEDURE`handle_system_dict`()BEGIN #Routinebodygoesher......
  • day 1 数组 704.二分查找、27.移除元素
    704.二分查找题目链接:704.二分查找视频教程文章教程思路利用middle去寻找target前提条件:这道题目的前提是数组为有序数组,同时题目还强调数组中无重复元素,因为一旦有重复元素,二分查找法返回的元素下标可能就不唯一,这些都是二分法的前提,以后看到题目描述后可以先想一想......
  • TreeAPI 递归和非递归遍历
    只包含递归和非递归遍历#include<stdio.h>#include<stdlib.h>#defineMaxSize20typedefstructnode{intdata;structnode*lchild,*rchild;}TreeNode,*Tree;typedefTreeNode*Elem;//方便修改栈中元素类型typedefstruct{Elemdata......
  • 143-3 二叉树后序非递归遍历
    二叉树的后序非递归遍历使用辅助栈 r指针的作用是判断该结点是否遍历过#include<stdio.h>#include<stdlib.h>#defineMaxSize20typedefstructnode{intdata;structnode*lchild,*rchild;}TreeNode,*Tree;typedefTreeNode*Elem;typedefstruct{......
  • 二分法查找
    二分法原理:使用二分法一定要是先排序好的数组,如果没有排序好,比较只有可能怎么找都找不到数组:10(下标0)11121314151617181920(下标10)通过二分法查找,例如需要找出19这个元素的下标:(0+10)/2-->中间元素的下标:5拿着中间这个元素和目标要......
  • Java Hutool递归解压文件
    importcn.hutool.core.io.FileUtil;importcn.hutool.core.util.ZipUtil;importjava.io.File;importjava.nio.charset.Charset;publicclassRecursiveUnzip{finalstaticFiledestDir=newFile("D:\\python\\newProject\\excel");pub......
  • python 递归遍历目录筛选特定文件名的文件
    #!/usr/bin/pythonimportosimportsyslist=[]folder_path="/root"defsearch_files(folder_path):search_string="测试"forroot,dirs,filesinos.walk(folder_path):fornameinfiles:ifsearch_strin......
  • 关于折半查找的某个例题的理解
    1-习题展示2-习题解决我们都知道折半查找就是比较中间的数,然后决定查找左边还是右边。那么,对于这个题,我们只需要将序列按照二叉排序树的条件画出来,就会发现,B选项有分叉出现,不是左拐右拐的那种分叉。答案就出来啦~......
  • 由于蚂蚁老师课程视频中博客园网站更新,代码不适用于现有环境,故网上查找更新:网上爬取博
    importjsonimportreimportrequestsfrombs4importBeautifulSoupfOut=open("博客爬取文章列表标题及地址.txt","w",encoding="utf8")foridxinrange(20):print("#"*50,idx+1)url="https://www.cnblogs.com/AggSite/......
  • c++如何读取txt文件内容
    一、c++文件流:fstream //文件流ifstream //输入文件流ofstream //输出文件流 二、文件路径的表示1、绝对路径:inf.open("d://DEV_C++//LogFile//游泳数据//LUYINGYAN1039_SensorLog.txt");   注意:双斜线"\\" 2、相对路径:对相对路径而言,路......