Note that there are some explanatory texts on larger screens.

plurals
  1. POPOSIX Program to search entire file system for a file
    text
    copied!<p>Hey everyone. I need to write a POSIX program to search through an entire file system for a specified file starting at the top directory. I've got some code which isn't done at all, but when I run it, and check to see if a particular file is a directory, it's saying this file which is not at all a directory is a directory and is trying to move into it, causing an error. I'm not sure how I can tell it that this type of file isn't a directory. </p> <p>Here's my code. I know it's not perfect and I could probably do some things differently in the way of getting the directory names and passing them into the function. Either way, I'm pretty sure I have to do this recursively. </p> <p>The file in question is /dev/dri/card0 and I'm running this from a Debian virtual machine. </p> <pre><code>#include &lt;sys/types.h&gt; #include &lt;sys/stat.h&gt; #include &lt;dirent.h&gt; #include &lt;unistd.h&gt; #include &lt;time.h&gt; #include &lt;stdint.h&gt; #include &lt;locale.h&gt; #include &lt;langinfo.h&gt; #include &lt;fcntl.h&gt; #include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;string&gt; using namespace std; void SearchDirectory(string file_Name, string directory){ string new_Directory = directory; DIR *dirp; dirp = opendir(directory.c_str()); struct dirent *dptr; struct stat statStruct; while(dptr = readdir(dirp)){ stat(dptr-&gt;d_name, &amp;statStruct); if( S_ISDIR(statStruct.st_mode) ){ string check = dptr-&gt;d_name; if ( check.compare(".") == 0 || check.compare("..") == 0 ){ continue; } else{ cout &lt;&lt; dptr-&gt;d_name &lt;&lt; " is is a directory" &lt;&lt; endl; new_Directory.append("/"); new_Directory.append(dptr-&gt;d_name); SearchDirectory(file_Name, new_Directory); } } else if( S_ISREG(statStruct.st_mode)){ string check = dptr-&gt;d_name; if( check.compare(file_Name) == 0){ cout &lt;&lt; "Found " &lt;&lt; file_Name &lt;&lt; " in " &lt;&lt; directory &lt;&lt; "/" &lt;&lt; endl; } } } } int main(int argc, char *argv[]){ if(argc &lt; 2 || argc &gt; 2){ cerr &lt;&lt; "This program will find the specified file." &lt;&lt; endl; cerr &lt;&lt; "Usage: mysearch &lt;filename&gt;" &lt;&lt; endl; return 1; } string file_Name = argv[1]; SearchDirectory(file_Name, "/"); return 0; } </code></pre>
 

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