directory_iterator Class

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at directory_iterator Class.

Describes an input iterator that sequences through the filenames in a directory. For an iterator X, the expression *X evaluates to an object of class directory_entry that wraps the filename and anything known about its status.

The class stores an object of type path, called mydir here for the purposes of exposition, which represents the name of the directory to be sequenced, and an object of type directory_entry called myentry here, which represents the current filename in the directory sequence. A default constructed object of type directory_entry has an empty mydir pathname and represents the end-of-sequence iterator.

For example, given the directory abc with entries def and ghi, the code:

for (directory_iterator next(path("abc")), end; next != end; ++next) visit(next->path());

will call visit with the arguments path("abc/def") and path("abc/ghi").

For more information and code examples, see File System Navigation (C++).

class directory_iterator;  

directory_iterator() noexcept;  
explicit directory_iterator(const path& pval);

directory_iterator(const path& pval,  
    error_code& ec) noexcept;  
directory_iterator(const directory_iterator&) = default;  
directory_iterator(directory_iterator&&) noexcept = default;  

The first constructor produces an end-of-sequence iterator. The second and third constructors store pval in mydir, then attempt to open and read mydir as a directory. If successful, they store the first filename in the directory in myentry; otherwise they produce an end-of-sequence iterator.

The defaulted construtors behave as expected.

directory_iterator& increment(error_code& ec) noexcept;  

The function attempts to advance to the next filename in the directory. If successful, it stores that filename in myentry; otherwise it produces an end-of-sequence iterator.

bool operator!=(const directory_iterator& right) const;

The member operator returns !(*this == right).

directory_iterator& operator=(const directory_iterator&) = default;  
directory_iterator& operator=(directory_iterator&&) noexcept = default;  

The defaulted member assignment operators behave as expected.

bool operator==(const directory_iterator& right) const;

The member operator returns true only if both *this and right are end-of-sequence iterators or both are not end-of-sequence-iterators.

const directory_entry& operator*() const;

The member operator returns myentry.

const directory_entry * operator->() const;

The member function returns &**this.

directory_iterator& operator++();

directory_iterator& operator++(int);

The first member function calls increment(), then returns *this. The second member function makes a copy of the object, calls increment(), then returns the copy.

Header: <experimental/filesystem>

Namespace: std::experimental::filesystem::v1

Header Files Reference
<filesystem>
File System Navigation (C++)

Show: