next up previous
Next: Get official Host name Up: Unix & Linux Previous: mmap

Directory reading

int file_select(const struct dirent *entry) 
{
    return (strcmp(entry->d_name,"Target.pb") == 0);
}

int main() 
{
    char pathname[128];

    // get the current directory name
    if (getwd(pathname) == NULL) {
        cerr << "error" << strerror(errno) << endl;
        return 1;
    }

    struct dirent **files;
    int count = scandir(pathname, &files, file_select,NULL);

    cout << "find " << count << " files under " << pathname << endl;
    for (int i = 0; i < count; ++i)
        cout << files[i]->d_name << endl;

    return 0;
}



Wei Lu 2007-11-06