1

I want to create a loop that iterates through all folders and files in a path iteratively. I have the following code (as described here)

#include <filesystem> 
#include <iostream>

for (const auto& dirEntry : std::filesystem::recursive_directory_iterator("some path here")) {
        std::cout << dirEntry;
    }

However, I am getting the following error:

'<<': no operator found which takes a right-hand operand of type 'const std::filesystem::directory_entry' (or there is no acceptable conversion)

Can anybody explain how I can fix this? I am using C++17

  • 1
    You need to define how you want directory iterators to be printed. Easiest to just convert to string, if that's what you need. – Mansoor Dec 18 '20 at 14:03
  • Try: `std::cout << dirEntry.path();` – jignatius Dec 18 '20 at 14:06
  • My implementation (gcc version 10.2.0 (Rev5, Built by MSYS2 project)) has a defined `operator<<` and your code works, copy-pasted, but when you dive into the implementation you can see a comment just above the definiton of the `<<`, which says: `// _GLIBCXX_RESOLVE_LIB_DEFECTS // 3171. LWG 2989 breaks directory_entry stream insertion`. Additionally, I did not find anything in the standard which would even hint that there is an `operator<<` defined for `directory_entry`. In that case you'd need to use `.path()` or workarounds like that. – Fureeish Dec 18 '20 at 14:10
  • Thanks for your answers. `std::cout << dirEntry.path();` indeed works. However, I am now wondering why I cannot convert `dirEntry.path().filename()` to a string like `std::string file = dirEntry.path().filename()`. Shouldn't `dirEntry.path().filename()` be of type string?. The error is 'initializing': cannot convert from `'std::filesystem::path' to 'std::basic_string,std::allocator>'` – masteryoda436 Dec 18 '20 at 14:41
  • @masteryoda436 To convert filename to string use the `string()` method of `std::filesystem::path`. https://en.cppreference.com/w/cpp/filesystem/path – jignatius Dec 18 '20 at 15:08

1 Answers1

0

there is no overload in the standard library for operator<< which takes a right hand operator of type std::filesystem::directory_entry but there is exist one for std::filesystem::path which you can obtain from std::filesystem::directory_entry using path() method, so all should you do is to convert this line :

std::cout << dirEntry;

to :

std::cout << dirEntry.path();

dev65
  • 1,440
  • 10
  • 25
  • Thanks for your answer. This indeed works. However, I am now wondering why I cannot convert `dirEntry.path().filename()` to a string like `std::string file = dirEntry.path().filename()`. Shouldn't `dirEntry.path().filename()` be of type string?. The error is `'initializing': cannot convert from 'std::filesystem::path' to 'std::basic_string,std::allocator>'` – masteryoda436 Dec 18 '20 at 14:35
  • @masteryoda436 You'll have better luck if you stop guessing, and consult your favourite documentation instead to find out how to use these functions. – Asteroids With Wings Dec 18 '20 at 14:43
  • You are right, sorry. For people who are interested: `dirEntry.path().filename().string()` worked for me. – masteryoda436 Dec 18 '20 at 15:26