读目录

int IBC_InfoIndb::read_file_from_dir(const char* path,char filename[][256],int max_size
,int* file_cnt)
{
    DIR* dir=NULL;
    struct   dirent*   entry;
    cout<<"read dir :"<<path<<endl;
    dir=opendir(path);
    if(!dir)
    {
        fprintf(stderr,"opendir %s fail!\n",path);
        return -1;
    }
    *file_cnt=0;
    readdir(dir);
    readdir(dir);
    while(entry=readdir(dir))
    {
        if(*file_cnt>max_size-1)
        {
            closedir(dir);
            return 1;
        }
        if(entry->d_name[0]=='~'||strcmp(strrchr(entry->d_name,'.')+1,"err")==0||
            strcmp(strrchr(entry->d_name,'.')+1,"del")==0)
            continue;
        string str(path);
        str=str+"/"+entry->d_name;
        struct stat buf;
        stat(str.c_str(),&buf);
        if(!S_ISREG(buf.st_mode))
            continue;
        strcpy(filename[*file_cnt],entry->d_name);
        *file_cnt=*file_cnt+1;
    }
    closedir(dir);
    return 0;
}
原文地址:https://www.cnblogs.com/hbt19860104/p/2626405.html