// linux 文件夹下文件过滤#include
#include
#include
#include
#include
#define DEBUG(args...) \
do{ \
char b__[100]; \
sprintf(b__,args); \
printf("%s %d %s\n",__FILE__,__LINE__,b__); \
}while(0)
typedef struct file_t f_info;
struct file_t{
char file_name[20];
f_info *next;
};
f_info* get_file_count(char *path)
{
DIR *dir;
int total = 0;
struct dirent* ptr;
f_info* file_info =NULL,*tmp=NULL;
f_info* info = (f_info *)malloc(sizeof(f_info));
file_info = info;
if(NULL == (dir = opendir(path))){
DEBUG("File open dir fail...");
exit(1);
}
while((ptr = readdir(dir)) !=NULL){
if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0)
continue;
if(ptr->d_type == DT_DIR)
continue;
if(ptr->d_type == DT_REG){
int size = strlen(ptr->d_name);
if(strcmp(ptr->d_name+(size - 3),".ts") == 0){
tmp = (f_info *)malloc(sizeof(f_info));
sprintf(tmp->file_name,ptr->d_name);
info = info->next = tmp;
//DEBUG("------%s-----",info->file_name);
}
}
}
info = info->next = NULL;
closedir(dir);
return file_info;
}
int main(void)
{
f_info *file_info;
int size;
file_info = get_file_count("./");
while(file_info=file_info->next){
DEBUG("%s ",file_info->file_name);
}
return 0;
}