Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Multi threaded directory scan code
    primarykey
    data
    text
    <p>I was looking how to write a multi threaded C++ code for scanning directory and get list of all files underneath. I have written a single threaded code which can do and below the code which can do that.</p> <pre><code>#include &lt;sys/types.h&gt; #include &lt;dirent.h&gt; #include &lt;errno.h&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;iostream&gt; #include &lt;sys/stat.h&gt; /* for stat() */ using namespace std; int isDir(string path) ; /*function... might want it in some class?*/ int getdir (string dir, vector&lt;string&gt; &amp;dirlist, vector&lt;string&gt; &amp;fileList) { DIR *dp; struct dirent *dirp, *dirFp ; if((dp = opendir(dir.c_str())) == NULL) { cout &lt;&lt; "Error(" &lt;&lt; errno &lt;&lt; ") opening " &lt;&lt; dir &lt;&lt; endl; return errno; } while ((dirp = readdir(dp)) != NULL) { if (strcmp (dirp-&gt;d_name, ".") != 0 &amp;&amp; strcmp(dirp-&gt;d_name, "..") != 0) { //dirlist.push_back(string(dirp-&gt;d_name)); string Tmp = dir.c_str()+ string("/") + string(dirp-&gt;d_name); if(isDir(Tmp)) { //if(isDir(string(dir.c_str() + dirp-&gt;d_name))) { dirlist.push_back(Tmp); getdir(Tmp,dirlist,fileList); } else { // cout &lt;&lt; "Files :"&lt;&lt;dirp-&gt;d_name &lt;&lt; endl; fileList.push_back(string(Tmp)); } } } closedir(dp); return 0; } int isDir(string path) { struct stat stat_buf; stat( path.c_str(), &amp;stat_buf); int is_dir = S_ISDIR( stat_buf.st_mode); // cout &lt;&lt;"isDir :Path "&lt;&lt;path.c_str()&lt;&lt;endl; return ( is_dir ? 1: 0); } int main() { string dir = string("/test1/mfs"); vector&lt;string&gt; dirlist = vector&lt;string&gt;(); vector&lt;string&gt; fileList = vector&lt;string&gt;(); getdir(dir,dirlist,fileList); #if 0 for (unsigned int i = 0;i &lt; dirlist.size();i++) { cout &lt;&lt; "Dir LIst" &lt;&lt;dirlist[i] &lt;&lt; endl; //string dirF = dir + "/" + dirlist[i]; //getdir(dirF,fileList); } #endif for (unsigned int i = 0; i &lt; fileList.size(); i++) cout &lt;&lt; "Files :"&lt;&lt;fileList[i]&lt;&lt; endl; return 0; } </code></pre> <p>Now issue is that it is single threaded and I need to scan say about 8000 directories under which file can be present. So I am not getting how to do so as number of directories can vary as it is decided by N dimension matrix.</p> <p>Any help in this regard will be great. Thanks in advance.</p>
    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.
 

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