
Hello, On 9/4/07, eric ehlers <eric.ehlers@gmail.com> wrote:
Hello,
Using boost version 1.34.1 I get an assertion failure when comparing recursive_directory_iterator to the end iterator if the corresponding path comprises an empty directory.
// a directory that is not empty - runs OK //boost::filesystem::path boostPath("C:\\test_notempty"); // a directory that is empty - crashes boost::filesystem::path boostPath("C:\\test_empty");
if (boost::filesystem::is_directory(boostPath)) { boost::filesystem::recursive_directory_iterator end_itr; for (boost::filesystem::recursive_directory_iterator itr(boostPath); itr != end_itr; ++itr) { std::cout << itr->path().string() << std::endl; } }
Assertion failed: m_imp.get() && "attempt to dereference end iterator", file c:\ program files\boost\boost_1_34_1\boost\filesystem\operations.hpp, line 849
This issue has been reported before:
http://lists.boost.org/Archives/boost/2006/10/111230.php
What's the workaround? I can't seem to find any way to inspect the path or its iterator to distinguish an empty directory from a nonempty one without crashing the program.
On further analysis it seems that, with an empty directory, comparing a recursive iterator to the end iterator returns false, while performing the same comparison with a normal iterator returns true. boost::filesystem::path p1("C:\\test_notempty"); boost::filesystem::path p2("C:\\test_empty"); // nonempty directory, normal iterator - outputs 0 cout << (boost::filesystem::directory_iterator(p1) == boost::filesystem::directory_iterator()) << endl; // empty directory, normal iterator - outputs 1 cout << (boost::filesystem::directory_iterator(p2) == boost::filesystem::directory_iterator()) << endl; // nonempty directory, recursive iterator - outputs 0 cout << (boost::filesystem::recursive_directory_iterator(p1) == boost::filesystem::recursive_directory_iterator()) << endl; // empty directory, recursive iterator - outputs 0 cout << (boost::filesystem::recursive_directory_iterator(p2) == boost::filesystem::recursive_directory_iterator()) << endl; So I guess that before using a recursive iterator to iterate over a directory you must first use a normal iterator to test whether the directory is empty. Regards, Eric