ftw不会遍历子目录c ++

时间:2014-02-12 20:25:23

标签: c++

嘿,每一个我都有这段代码,我试图遍历一个目录树,但它只会通过1个子目录。任何人都可以帮我尝试让这段代码遍历树中的所有子目录吗?

static int callback(const char *fpath, const struct stat *sb, int typeflag) {
    /* if it's a file */
    if (typeflag == FTW_F) {

        /* for each filter, */
        string basepath = fpath;
        basepath = basepath.substr(0, basepath.find_last_of('/')-1);
        cout << "basepath: " << basepath << endl;


        for (int i = 0; i < sizeof(filters) / sizeof(filters[0]); i++) {
            string finpath = basepath + string(filters[i]);
            cout << "finpath: " << finpath << endl;
            //string path = basepath + string(filters[i]);
            // if the filename matches the filter, 
            if (fnmatch(finpath.c_str(), fpath, FNM_CASEFOLD) == 0) {
                cout << fpath << endl;
                break;
            }
        }
    }

    /* tell ftw to continue */
    return 0;
}

0 个答案:

没有答案