2012-04-11 6 views
0

walkThroughFunctionの再帰呼び出しに問題があります。 コードはディレクトリを通過し、サブディレクトリをカウントし、ファイルが見つかった場合はそれを開き、特定の文字列を検索する必要があります。 コードは1つのディレクトリのみを経由します。誰かがこれで私を助けることができますか?中括弧が少しずれていることがわかります。親切にそれらを無視してください。再帰ディレクトリとファイルストリーミングと検索文字列

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<<"Error"<<endl; 
    cout<<directory_name<<" Cannot be Opened"<<endl; 
    exit(10000); 
} 
while((walker=readdir(directory)) !=NULL){ 




    strcpy(d_name,walker->d_name); 
    cout<<directory_name<<"/"<<endl; 
    if (strcmp (d_name, "..") == 0 && 
      strcmp (d_name, ".") == 0){ 
     continue; 
    } 
    else{ 


     path_length =  snprintf(path,PATH_MAX,"%s/%s\n",directory_name,d_name); 
     cout<<"HELLO"<<endl; 
     cout<<path<<endl; 
     if (path_length >= PATH_MAX){ 
      cout<<"Path is too long"<<endl; 
      exit (1000); 
     } 
     if(walker->d_type==DT_DIR){ 
      cout<<"Hello"<<endl; 
      directories++; 
      walkThroughDirectory (path,searchString); 
     } 
     else if(walker->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<<path<<": "<<currentLine<<": "<<line<<endl; 
       } 
      } 
      openFile.close();  
     } 
     /* 
     struct stat directory_stat; 
     if (stat(path, &directory_stat) == -1){ 

      return; 
     } 
     if (S_ISDIR(directory_stat.st_mode)){ 
      cout<<"HELLO"<<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<<path<<": "<<currentLine<<": "<<line<<endl; 

       } 

      } 
      // it's a file so search for text in file 

     } 
     */ 

    } 

} 

if (closedir (directory)) 
{ 
    cout<<"Unable to close "<<directory_name<<endl; 
    exit (1000); 
} 
} 

int main(){ 

    char * name; 
    name=new char; 

    cout<<"Total Directories "<< directories<<endl; 



    name=get_current_dir_name(); 
    cout<<"Current Directory is: "<<name<<endl; 
    /* 
    cout<<"Now Enter The Desired Directory from the root or the current path"<<endl; 
    char *desiredDirectory; 
    desiredDirectory=new char; 
    cin>>desiredDirectory; 
    cout<<"Enter The String You want to search"<<endl; 
    char *searchString; 
    searchString=new char; 
    cin>>searchString; 
    */ 
    char ourpath[400]; 
    strcpy(ourpath,name); 

    walkThroughDirectory(ourpath,"diminutive"); 
    cout<<"Total Directories "<< directories<<endl; 


    return 0; 
} 
+0

なぜあなたの括弧は間違っていますか?適切なインデントスキームを使用すると、多くのエラーをより迅速に修正できます。 – Bojangles

+0

私は急いで問題を解決しようとしていました。それはプロジェクトであり、できるだけ早くそれを与えなければならないからです。したがって、置き忘れ。すみません、あなたは@JamWafflesを助けることができます – mobykhn

答えて

0

このコードにはいくつかの問題があります。まず、strcmpを実行して、d_nameが "。"であるかどうかを確認します。または ".."の場合は、ANDを使用する必要はありません。第二に、sprintfを呼び出してC文字列pathを作成するときは、文字列の最後に改行を入れてはいけません。これは、あなたのコードが1レベルだけ深くなる原因となっていました。第三に、get_current_dir_nameに電話すると、mallocがあなたのために働きます。あなたがやっていることは、それ自身では機能しないAPIの正しい使い方ではない単一のcharのためのスペースを割り当てることです。 man page for get_current_dir_nameを参照してください。

以下のコードは、これらの問題に対処しています(適切なインデントもあります)。

#include <iostream> 
#include <fstream> 
#include <stdlib.h> 
#include <unistd.h> 
#include <dirent.h> 
#include <limits.h> 
#include <string.h> 

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) 
    { 
     std::cout << directory_name << " Cannot be Opened" << std::endl; 
     exit(1); 
    } 

    while((walker=readdir(directory)) != NULL) 
    { 
     strcpy(d_name, walker->d_name); 

     // Needs to be || not && 
     if (strcmp(d_name, "..") == 0 || strcmp(d_name, ".") == 0) 
     { 
      continue; 
     } 
     else 
     { 
      // No newline on the path name. 
      path_length = snprintf(path, PATH_MAX, "%s/%s", directory_name, d_name); 

      if (path_length >= PATH_MAX) 
      { 
       std::cout << "Path is too long" << std::endl; 
       exit(2); 
      } 

      if(walker->d_type == DT_DIR) 
      { 
       directories++; 
       walkThroughDirectory(path, searchString); 
      } 
      else if(walker->d_type==DT_REG) 
      { 
       std::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) 
         std::cout << path << ": " << currentLine << ": " << line << std::endl; 
        } 
       } 
       openFile.close();  
      } 
     } 
    } 

    if (closedir(directory)) 
    { 
     std::cout << "Unable to close " << directory_name << std::endl; 
     exit(3); 
    } 
} 

int main() 
{ 
    // get_current_dir_name() mallocs a string for you. 
    char *name; 

    name = get_current_dir_name(); 
    walkThroughDirectory(name, "matthew"); 
    free(name); 
    std::cout << "Total Directories: " << directories << std::endl; 

    return 0; 
}