linux c++带通配找文件 枚举文件 目录

该博客介绍了如何使用C++在Linux环境下实现通配符查找文件和目录的功能。通过枚举文件系统,利用EnumWildCharMatch函数进行通配符匹配,并提供了一个名为EnumFiles的函数,该函数可以指定路径、过滤器、是否包含子目录、是否清除输出数据以及要搜索的文件数量。在main函数中展示了如何使用这个函数来查找/usr/lib目录下的.so文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <vector>
#include <string>
#include <stack>
using namespace std;

typedef std::vector<std::string> filePathArr;

enum EnumFileType
{
  EnumFileType_File,
  EnumFileType_Directory
};

#define MATCH_CHAR(c1,c2,ignore_case)  ( (c1==c2) || ((ignore_case) &&(tolower(c1)==tolower(c2))) )

//EnumWildCharMatch通配匹配函数来自于:http://blog.youkuaiyun.com/c80486/article/details/6584769
//感谢作者让我方便不少,谢谢
bool EnumWildCharMatch(const char *src, const char *pattern, bool ignore_case)
{
	bool result = false;

	while (*src)
	{
		if (*pattern == '*')
		{
			  /* 如果 pattern 的当前字符是 '*' */
			/* 如果后续有多个 '*', 跳过 */
			while ((*pattern == '*') || (*pattern == '?'))
				pattern++;

			/* 如果 '*" 后没有字符了,则正确匹配 */
			if (!*pattern) return true;

			/* 在 src 中查找一个与 pattern中'*"后的一个字符相同的字符*/
			while (*src && (!MATCH_C
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值