读目录

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;
}

posted on 2012-08-07 11:26  山本二十八  阅读(128)  评论(0编辑  收藏  举报

导航