Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Interestingly, I had the same idea some time ago. Here's what I wrote up:</p> <pre><code>#define WIN32_LEAN_AND_MEAN #include &lt;windows.h&gt; #include &lt;string&gt; #include &lt;iterator&gt; #include &lt;exception&gt; #ifndef DIR_ITERATOR_H_INC #define DIR_ITERATOR_H_INC class dir_iterator #if (!defined(_MSC_VER)) || (_MSC_VER &gt; 1200) : public std::iterator&lt;std::input_iterator_tag, std::string, int, std::string *, std::string &amp;&gt; #endif { mutable HANDLE it; std::string mask; std::string path; WIN32_FIND_DATA data; bool done; DWORD require; DWORD prohibit; public: WIN32_FIND_DATA operator*() { return data; } dir_iterator(dir_iterator const &amp;other) : it(other.it), mask(other.mask), path(other.path), data(other.data), done(other.done), require(other.require), prohibit(other.prohibit) { // Transfer the handle instead of just copying it. other.it=INVALID_HANDLE_VALUE; } dir_iterator(std::string const &amp;s, DWORD must = 0, DWORD cant = FILE_ATTRIBUTE_DIRECTORY) : mask(s), require(must), prohibit(cant &amp; ~must), done(false), it(INVALID_HANDLE_VALUE) // To fix bug spotted by Billy ONeal. { int pos; if (std::string::npos != (pos=mask.find_last_of("\\/"))) path = std::string(mask, 0, pos+1); it = FindFirstFile(mask.c_str(), &amp;data); if (it == INVALID_HANDLE_VALUE) throw std::invalid_argument("Directory Inaccessible"); while (!(((data.dwFileAttributes &amp; require) == require) &amp;&amp; ((data.dwFileAttributes &amp; prohibit) == 0))) { if (done = (FindNextFile(it, &amp;data)==0)) break; } } dir_iterator() : done(true) {} dir_iterator &amp;operator++() { do { if (done = (FindNextFile(it, &amp;data)==0)) break; } while (!(((data.dwFileAttributes &amp; require) == require) &amp;&amp; (data.dwFileAttributes &amp; prohibit) == 0)); return *this; } bool operator!=(dir_iterator const &amp;other) { return done != other.done; } bool operator==(dir_iterator const &amp;other) { return done == other.done; } ~dir_iterator() { // The rest of the bug fix -- only close handle if it's open. if (it!=INVALID_HANDLE_VALUE) FindClose(it); } }; #endif </code></pre> <p>And a quick demonstration of it:</p> <pre><code>#include "dir_iterator.h" #include &lt;iostream&gt; #include &lt;algorithm&gt; namespace std { std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, WIN32_FIND_DATA const &amp;d) { return os &lt;&lt; d.cFileName; } } int main() { std::copy(dir_iterator("*"), dir_iterator(), std::ostream_iterator&lt;WIN32_FIND_DATA&gt;(std::cout, "\n")); std::cout &lt;&lt; "\nDirectories:\n"; std::copy(dir_iterator("*", FILE_ATTRIBUTE_DIRECTORY), dir_iterator(), std::ostream_iterator&lt;WIN32_FIND_DATA&gt;(std::cout, "\n")); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload