Boost C++ Libraries: Ticket #8946: filesystem::directory_iterator returns incorrect listing https://svn.boost.org/trac10/ticket/8946 <p> Both <strong>directory_iterator</strong> and <strong>recursive_directory_iterator</strong> do not work correctly under linux with g++-4.7.3 : one item, assumed to be the last one, is not listed. </p> <p> Simple code to reproduce the problem : </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;boost/filesystem.hpp&gt; using namespace std; namespace bfs = boost::filesystem; int main () { system("mkdir mydir"); system("touch mydir/file1"); system("touch mydir/file2"); bfs::directory_iterator bfs_dir_i_end; bfs::directory_iterator bfs_dir_i("mydir/."); while (++bfs_dir_i != bfs_dir_i_end) { bfs::file_type type = (*bfs_dir_i).status().type(); bfs::path source = (*bfs_dir_i).path(); cout &lt;&lt; source &lt;&lt; " " &lt;&lt; type &lt;&lt; endl; } } </pre><p> Build with : </p> <pre class="wiki">g++-4.7.3 -I/usr/include/crypto++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 show_bug_directory_iterator.cpp g++-4.7.3 -lboost_filesystem -o show_bug_directory_iterator show_bug_directory_iterator.o </pre><p> Code result : </p> <pre class="wiki">$ ./show_bug_directory_iterator "mydir/./file1" 2 </pre><p> Should be : </p> <pre class="wiki">$ ./show_bug_directory_iterator "mydir/./file1" 2 "mydir/./file2" 2 </pre><p> Shell result : </p> <pre class="wiki">$ ls -lR . .: total 1620 drwxr-xr-x 2 alain alain 4096 31 juil. 10:48 mydir -rwxr-xr-x 1 alain alain 639766 31 juil. 10:48 show_bug_directory_iterator -rw-r--r-- 1 alain alain 497 31 juil. 10:44 show_bug_directory_iterator.cpp -rw-r--r-- 1 alain alain 1006376 31 juil. 10:48 show_bug_directory_iterator.o ./mydir: total 0 -rw-r--r-- 1 alain alain 0 31 juil. 10:48 file1 -rw-r--r-- 1 alain alain 0 31 juil. 10:48 file2 </pre><p> Notes : </p> <blockquote> <p> 1) Not tested under other gcc versions. </p> </blockquote> <blockquote> <p> 2) Tested with same results on Boost 1.52.0 and 1.53.0 </p> </blockquote> <blockquote> <p> 3) suspected regression, cf <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/257"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/257</a> </p> </blockquote> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8946 Trac 1.4.3 anonymous Wed, 31 Jul 2013 10:41:23 GMT version changed https://svn.boost.org/trac10/ticket/8946#comment:1 https://svn.boost.org/trac10/ticket/8946#comment:1 <ul> <li><strong>version</strong> <span class="trac-field-old">Boost 1.53.0</span> → <span class="trac-field-new">Boost 1.54.0</span> </li> </ul> Ticket l.alebarde@… Wed, 31 Jul 2013 10:44:56 GMT <link>https://svn.boost.org/trac10/ticket/8946#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8946#comment:2</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8946#comment:1" title="Comment 1">anonymous</a>: Sorry, change made by myself. So, same result on versions 1.52.0, 1.53.0, and 1.54.0 </p> </description> <category>Ticket</category> </item> <item> <author>nikita.trophimov@…</author> <pubDate>Sat, 03 Aug 2013 16:03:51 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8946#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8946#comment:3</guid> <description> <p> You need to increment bfs_dir_i after all of the actions in the loop: </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;boost/filesystem.hpp&gt; using namespace std; namespace bfs = boost::filesystem; int main () { system("mkdir mydir"); system("touch mydir/file1"); system("touch mydir/file2"); bfs::directory_iterator bfs_dir_i_end; bfs::directory_iterator bfs_dir_i("mydir"); while (bfs_dir_i != bfs_dir_i_end) { bfs::file_type type = (*bfs_dir_i).status().type(); bfs::path source = (*bfs_dir_i).path(); cout &lt;&lt; source &lt;&lt; " " &lt;&lt; type &lt;&lt; endl; ++bfs_dir_i; } } </pre> </description> <category>Ticket</category> </item> <item> <author>l.alebarde@…</author> <pubDate>Wed, 07 Aug 2013 08:26:09 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8946#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8946#comment:4</guid> <description> <p> Thanks nikita. The documentation should be modified then (<a href="http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html#Class-directory_iterator">http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html#Class-directory_iterator</a>): </p> <pre class="wiki">To iterate over the current directory, use directory_iterator(".") </pre><p> This lead me to increment first the iterator. </p> </description> <category>Ticket</category> </item> </channel> </rss>