简体   繁体   中英

how to choose a specific direcrory entry within directory iterator

My question is very specific, sorry for that.

From this code:

int i = 0;
for(auto& p: recursive_directory_iterator("stru1")) {
    ++i;
    cout << "Index no." << i <<  p.path() << '\n';

in this the loop will iterates over every entity in the directory and output the path with their index number.

My question is if there is anyway where, after the loop is done, I can choose a specific index and display the content of the index's directory?

The only way to be certain you get the same entry (if there are concurrent modifications to that directory) would be to build a vector during iteration:

std::size_t i = 0;
std::vector<std::filesystem::directory_entry> entries;
for(auto& p: std::filesystem::recursive_directory_iterator("stru1")) {
    ++i;
    std::cout << "Index no." << i <<  p.path() << '\n';
    entries.push_back(p);
}

if (std::cin >> i && i >= 1 && i <= entries.size()) {
    auto const & entry = entries[i - 1];
    // use entry
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM