其实很多种办法,可以使用dir相关的函数把文件都枚举出来,然后使用正则表达式,这个是我之前的做法。但是有更简单的办法,就是使用glob库, 示例实现如下:
#include <glob.h>
void GetAllPathsByPattern(const char * pattern)
{
glob_t resolved_paths;
memset(&resolved_paths, 0, sizeof(resolved_paths));
if (glob(pattern, GLOB_TILDE, NULL, &resolved_paths) == 0) {
for(size_t j = 0; j < resolved_paths.gl_pathc; ++j) {
const char* resolved_path = resolved_paths.gl_pathv[j];
//
printf("file %s \n", resolved_path);
}
} else {
ok = false;
}
globfree(&resolved_paths);
}
使用方法:
GetAllPathsByPattern("/data/*.assets");
如此简单,没有想到吧?