转自https://blog.csdn.net/a503932194/article/details/47830779
#include<iostream>
#include<stdio.h>
#include<unistd.h>
#include<dirent.h>
#include<string.h>
#include<cstring>
#include<stdlib.h>
using namespace std;
/*
参数 第一解析目录地址 第二 截取后存放的数组 第三分隔符即解析后缀
方法返回数组实际长度,注意size-3部分其中3是分隔符长度
解析失败返回-1
*/
int searchdir(const char *path, char *filename[],const char *split)
{
//目录
DIR *dp;
//获取dir目录具体的文件信息,名字,长度,指针地址等
struct dirent *dirp;
if ((dp = opendir(path)) == NULL)
{
perror("opendir");
}
int strL = 0;
while ((dirp = readdir(dp)) != NULL)
{
if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
continue;
int size = strlen(dirp->d_name);
if (strcmp((dirp->d_name + ( size - 3)), ".in")!=0)
continue;
filename[strL++] = strtok(dirp->d_name, split);
}
closedir(dp);
return strL;
}