Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursive Directories and File streaming and Searching Strings
    primarykey
    data
    text
    <p>I have an issue with the recursive call in the walkThroughFunction. The code is supposed to go through directories and count the sub directories and if it finds a file it should open it and search a certain string. The code only goes through one directory. Can someone help me with this. You will find the braces misplaced a little. Kindly ignore those.</p> <pre><code>int directories=0; void walkThroughDirectory(char *directory_name,char *searchString){ DIR * directory; struct dirent * walker; char d_name[PATH_MAX]; int path_length; char path[PATH_MAX]; directory=opendir(directory_name); if(directory==NULL){ cout&lt;&lt;"Error"&lt;&lt;endl; cout&lt;&lt;directory_name&lt;&lt;" Cannot be Opened"&lt;&lt;endl; exit(10000); } while((walker=readdir(directory)) !=NULL){ strcpy(d_name,walker-&gt;d_name); cout&lt;&lt;directory_name&lt;&lt;"/"&lt;&lt;endl; if (strcmp (d_name, "..") == 0 &amp;&amp; strcmp (d_name, ".") == 0){ continue; } else{ path_length = snprintf(path,PATH_MAX,"%s/%s\n",directory_name,d_name); cout&lt;&lt;"HELLO"&lt;&lt;endl; cout&lt;&lt;path&lt;&lt;endl; if (path_length &gt;= PATH_MAX){ cout&lt;&lt;"Path is too long"&lt;&lt;endl; exit (1000); } if(walker-&gt;d_type==DT_DIR){ cout&lt;&lt;"Hello"&lt;&lt;endl; directories++; walkThroughDirectory (path,searchString); } else if(walker-&gt;d_type==DT_REG){ ifstream openFile; openFile.open(path); char line[1500]; int currentLine = 0; if (openFile.is_open()){ while (openFile.good()){ currentLine++; openFile.getline(line, 1500); if (strstr(line, searchString) != NULL) cout&lt;&lt;path&lt;&lt;": "&lt;&lt;currentLine&lt;&lt;": "&lt;&lt;line&lt;&lt;endl; } } openFile.close(); } /* struct stat directory_stat; if (stat(path, &amp;directory_stat) == -1){ return; } if (S_ISDIR(directory_stat.st_mode)){ cout&lt;&lt;"HELLO"&lt;&lt;endl; directories++; walkThroughDirectory(path, searchString); } else if (S_ISREG(directory_stat.st_mode)){ ifstream openFile; openFile.open(path); char line[1500]; int currentLine = 0; if (openFile.is_open()){ while (openFile.good()){ currentLine++; openFile.getline(line, 1500); if (strstr(line, searchString) != NULL) cout&lt;&lt;path&lt;&lt;": "&lt;&lt;currentLine&lt;&lt;": "&lt;&lt;line&lt;&lt;endl; } } // it's a file so search for text in file } */ } } if (closedir (directory)) { cout&lt;&lt;"Unable to close "&lt;&lt;directory_name&lt;&lt;endl; exit (1000); } } int main(){ char * name; name=new char; cout&lt;&lt;"Total Directories "&lt;&lt; directories&lt;&lt;endl; name=get_current_dir_name(); cout&lt;&lt;"Current Directory is: "&lt;&lt;name&lt;&lt;endl; /* cout&lt;&lt;"Now Enter The Desired Directory from the root or the current path"&lt;&lt;endl; char *desiredDirectory; desiredDirectory=new char; cin&gt;&gt;desiredDirectory; cout&lt;&lt;"Enter The String You want to search"&lt;&lt;endl; char *searchString; searchString=new char; cin&gt;&gt;searchString; */ char ourpath[400]; strcpy(ourpath,name); walkThroughDirectory(ourpath,"diminutive"); cout&lt;&lt;"Total Directories "&lt;&lt; directories&lt;&lt;endl; return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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