glibc 递归遍历文件夹 dfs_search_file

本文详细探讨了glibc中的dfs_search_file函数,该函数用于深度优先搜索(DFS)遍历指定文件夹,递归查找目标文件。通过解析源代码,分析了其工作原理,包括目录遍历、文件判断和递归调用等关键步骤,为理解和使用该功能提供了清晰的指导。
摘要由CSDN通过智能技术生成

dfs_search_file




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

int dfs_search_file (const char *dir)
{
  DIR *dp;
  struct dirent *ep;
  struct stat statbuf;
  
  dp = opendir (dir);
  if (dp != NULL)
    {
	    while ((ep = readdir (dp))!=NULL)
	    {
	        if(ep->d_type==4) 
	        {//is dir
	      		if (strcmp(ep->d_name , ".")==0 || strcmp(ep->d_name , "..")==0)
	      		//if (ep->d_name[strlen(ep->d_name)-1]=='.')
	      		{
	      			continue;
	      		}
	      		//
	      		char * tmp = (char *)malloc(sizeof(char) * (strlen(ep->d_name) + strlen(dir)+2 ));
	      		strcpy(tmp,dir);
	      		strcat(tmp,"/");
	      		strcat(tmp,ep->d_name);
	      		/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值