I am trying to use std::filesystem::recursive_directory_iterator
to iterate through my C drive and print file paths but I keep running into the following exception: terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error' what(): filesystem error: cannot increment recursive directory iterator: Invalid argument
I have tried:
#include <iostream>
#include <filesystem>
int main() {
for (auto const& dir_entry : std::filesystem::recursive_directory_iterator("C:\\")) {
try {
std::cout << dir_entry << std::endl;
} catch (std::filesystem::__cxx11::filesystem_error) {
continue;
}
}
return 0;
}
I was expecting it to continue to the next iteration after running into the exception but I simply received the same error message.
I also tried:
#include <iostream>
#include <filesystem>
int main() {
try {
for (auto const& dir_entry : std::filesystem::recursive_directory_iterator("C:\\")) {
std::cout << dir_entry << std::endl;
}
} catch (std::filesystem::__cxx11::filesystem_error) {
std::cout << "Ran into exception";
}
return 0;
}
which caught the problem and the catch
block was executed but I cannot continue to the next iteration because the try/catch
statements are outside of the for
loop.