scandir 使用示例

int filter_fn(const struct dirent * ent) {
    if (ent->d_type != DT_REG)
        return 0;
    return (strncmp(ent->d_name, "w_", strlen("w_")) == 0);
}
void SysWatcher::scan_log(const char * dir_name) {
    int n;
    struct dirent **namelist; // struct dirent * namelist[];
    n = scandir(dir_name, &namelist, filter_fn, alphasort);
    if (n < 0) {
        ALOGD(" SysWatcher scandir error! n<0 n=%d\n", n);
    }
    if (n == 0) {
        free(namelist);
    } else {
        for (int i = 0; i < n; i++) {
            printf("%s\n", namelist[i]->d_name);
            string fileName = string(namelist[i]->d_name);
            //ALOGD(LOG_TAG " SysWatcher old push %s------\n", fileName.c_str());
            logFiles.push(fileName);
            free(namelist[i]);
        }
        free(namelist);
    }
    return;
}

posted on 2015-09-08 14:44  小小鸟儿!  阅读(365)  评论(0编辑  收藏  举报