简体   繁体   中英

experimental::filesystem::directory_iterator& operator++()

I'm writing a program that takes all the file names in a directory and puts them in an array. The problem i'm having is the operator++() shows an error and won't increment the iterator. Any help would be greatly appreciated.

#include <iostream>
#include <string>
#include <filesystem>

namespace fs = std::experimental::filesystem;

int main()
{
    std::cout << "Select a directory :";
    std::string path;
    std::cin >> path;
    std::cout << "How many files :";
    int dirFiles;
    std::cin >> dirFiles;
    int i = { 0 };
    std::vector<std::string> fileNames(dirFiles);

    for (auto& p : fs::directory_iterator(path)){
        while (i < dirFiles) {
            fileNames[i] = p.path().string();
            fs::directory_iterator& operator++();
            std::cout << fileNames[i];
            i++;
        }
    }
    system("pause");
    return 0;
}

directory_iterator already knows how to loop over its constituent elements. You do not need to do additional work yourself:

std::vector<std::string> fileNames;

for (auto& p : fs::directory_iterator(path)){
    fileNames.push_back(p.path().string());
}

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