Reputation: 23
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;
}
Upvotes: 0
Views: 555
Reputation: 302718
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());
}
Upvotes: 1