0

I just started looking into C++ and have been reading a book and am only a couple of chapters in.

I thought a good exercise would be to print out a directory. When I look it up, I see this nice for loop that is driving the train from.

for (const auto & entry : fs::directory_iterator(path))
    std::cout << entry.path() << std::endl;

My next logical step was going to store these values in a vector or an array.

For the past three hours, I have not been able to figure it out. What is entry? Is it a constant? Is it a string? What's stopping me from placing it in a vector? I can't get this code to run.

I am sorry for the basic question, I just need some conceptual clarification.

This is my code, push_back() does not take constants and I can't convert the constants to a string. What am I missing?

auto filepath()
{
    std::vector <string> lis;
    std::string path = "C:/Users/Derek Comeau/3D Objects";
    
    for (const auto& entry : filesystem::directory_iterator(path)) {
        const string paths = entry.path();
        lis.push_back(paths);
    }
}
alfC
  • 14,261
  • 4
  • 67
  • 118
umfan110
  • 3
  • 2
  • This is your trail , https://en.cppreference.com/w/cpp/filesystem/directory_iterator -> reference : https://en.cppreference.com/w/cpp/filesystem/directory_entry path: https://en.cppreference.com/w/cpp/filesystem/directory_entry/path string: https://en.cppreference.com/w/cpp/filesystem/path/string – alfC Sep 23 '21 at 01:56

1 Answers1

5

A Range-based for loop iterates through a container using its iterators. In this case, there is no container, directory_iterator acts stand-alone.

When the directory_iterator is constructed, it finds the first file in the specified folder. When the loop dereferences the iterator via its operator*, it returns a const directory_entry& to the current file. When the loop advances the iterator via its operator++, it advances to the next file. Repeating until there are no more files to report.

The directory_entry::path() method returns a const path&, not a std::string. However, the path::string() method returns a std::string.

std::vector does not have a pushback() method. The correct name is push_back().

With that said, try this instead:

auto filepath()
{
    vector<std::string> lis;
    std::string folder = "C:/Users/Derek Comeau/3D Objects";

    for (const auto& entry : std::filesystem::directory_iterator(folder)) {
        lis.push_back(entry.path().string());
    }

    return lis;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks for the prompt reply ! Ok so directory_iterator returns an iterator that is stand alone and works well with a range based for loop. Is that a good way of thinking about it? Then within the for loop you can call the push_back method placing entry.path().string(), within a vector, which is an object that returns a string? – umfan110 Sep 24 '21 at 00:11
  • @umfan110 "*directory_iterator returns an iterator*" - no, it IS the iterator. "*and works well with a range based for loop*" - yes, because it implements [`std::begin()` and `std::end()`](https://en.cppreference.com/w/cpp/filesystem/directory_iterator/begin) overloaded functions for that exact purpose. – Remy Lebeau Sep 24 '21 at 00:18
  • Thanks Remy for the clarification. I think I understand now. – umfan110 Sep 24 '21 at 14:03