I have to create a vector of all files on my disk, starting from root ("C:/"). I used filesystem library and recursive_directory_iterator, however program throws filesystem::filesystem_error when it reaches some of system directories which only the system has access to.
#include <filesystem>
#include <iostream>
#include <vector>
using namespace std;
namespace fs = std::filesystem;
vector<fs::path> get_all_files_from_disk(fs::path path,vector<fs::path>* all_files = NULL) {
if (all_files == NULL)
all_files = new vector<fs::path>();
try {
for (auto dir : fs::directory_iterator(path)) {
if (dir.is_directory())
*all_files = get_all_files_from_disk(dir, all_files);
else
all_files->push_back(dir.path());
}
}
catch (fs::filesystem_error e) {
cout << "error";
}
return *all_files;
}
vector<fs::path> getAllFilesFromDisk(fs::path path) {
vector<fs::path> all_files;
for (auto dir : fs::recursive_directory_iterator(path)) {
if (!dir.is_directory())
all_files.push_back(dir.path());
};
return all_files;
};
int main() {
setlocale(LC_ALL, "");
fs::path path("C:/");
vector<fs::path> a = get_all_files_from_disk(path);
vector<fs::path> b = getAllFilesFromDisk(path);
}
The first algorithm doesn't crash, but executes really long time (it executed at about 40 minutes), however when output displaying all elements of vector is empty for some reason, with no-root directories it works fine. The second one crashes because of the reason stated in the beginning.
:( Could you help please?