2017-01-25 5 views
0

次のコードはパスを開き、ディレクトリを再帰的に読み込んでその中のファイルを出力します。現時点では、ディレクトリへのパスとその中のすべてのファイルを出力していますが、訪問したすべてのファイルへのフルパスを含む1つのchar *変数を含むリンクリストを実装したいと思います。この再帰関数をリンクされたリストにコンテンツを保存するにはどうすればよいですか?

#include <dirent.h> 
#include <stdio.h> 
#include <string.h> 

void show_dir_content(char * path) 
{ 
    DIR * d = opendir(path);     
    if(d==NULL) return;      
    struct dirent * dir;       
    while ((dir = readdir(d)) != NULL)  
    { 
     if(dir-> d_type != DT_DIR)   // if the type is not directory just print it 
      printf("\t%s\n",dir->d_name); 
     else 
      if(dir -> d_type == DT_DIR && strcmp(dir->d_name,".")!=0 && strcmp(dir->d_name,"..")!=0) // if it is a directory 
      { 
       char d_path[255];        // here I am using sprintf which is safer than strcat 
       sprintf(d_path, "%s/%s", path, dir->d_name); 
       printf("%s\n",d_path); 
       show_dir_content(d_path);      
      } 
    } 
    closedir(d);           
} 

int main(int argc, char **argv) 
{ 
    show_dir_content(argv[1]); 
    return(0); 
} 

リンクリストに使用する構造体のような非常に簡単になります:

ここにコードだ私は構造体のmallocをを使用して困難な時期を過ごしてい

typedef struct search search; 
struct search { 
    char *path; 
    char *fileName; 
    char *fullPathToFile; 
    search *next; 
}; 

はと再帰関数内で実際のリンクリストを作成します。どんな助けもありがとうございます。

+0

広すぎる質問にそれを渡します。あなたが抱えている正確な問題は何かを特定してください。 – user694733

+0

@ user694733私の問題は質問の最後に明記されています。 – user100000

+0

曖昧な問題のリストが表示されています。構造タイプを定義することができました。次のステップは何ですか?それが 'malloc'でメモリを割り当てているとすれば、あなたは何を持っているのですか?コンパイルエラーメッセージ?私たちが答えることができるという明確な質問があるまで、あなたの問題を小さな問題に分解してください。 – user694733

答えて

1

になります。新しいノードを入力して、リストの末尾に追加します。

const char *path_format = "%s/%s"; 

// Modified to take a node ptr. This should be the last node in the list 
// Returns a node ptr. This is the new last node in the list 
search * show_dir_content(char * path, search *node) 
{ 
    DIR * d = opendir(path);     
    if(d==NULL) return node;      
    struct dirent * dir;       
    while ((dir = readdir(d)) != NULL)  
    { 
     if(dir-> d_type != DT_DIR) { 
      // Found a file. Alloc a new search node and fill in 
      // (TODO: You should check the return from malloc for failure) 
      search *new_node = malloc(sizeof(search)); 
      // TODO: copy all the names. Hint: strdup 
      new_node->next = NULL; 
      // Append to end of list 
      node->next = new_node; 
      // Update node pointer to now point to the new node 
      node = node->next; 
     } 
     else 
      if(dir -> d_type == DT_DIR && strcmp(dir->d_name,".")!=0 && strcmp(dir->d_name,"..")!=0) // if it is a directory 
      { 
       // Not sure 255 chars will be enough.... 
       char d_path[255];        // here I am using sprintf which is safer than strcat 
       sprintf(d_path, path_format, path, dir->d_name); 
       printf("%s\n",d_path); 
       // Make sure you update the node pointer to reflect any 
       // changes made in the recursive call 
       node = show_dir_content(d_path, node); 
      } 
    } 
    closedir(d); 
    // Return the last node (this may be same as input parameter if no files found        
    return node; 
} 

更新mainは、ルート・ノードを作成し、機能

int main(int argc, char **argv) 
{ 
    search root = {0}; 
    show_dir_content(argv[1], &root); 
    // Note that root is a dummy node. 
    // The list actually begins at root->next 

    // Also, before you exit, free all mem 
    search *node = root.next, *next; 
    while (NULL != node) { 
     free(node->path); 
     free(node->fileName); 
     free(node->fullPathToFile); 
     next = node->next; 
     free(node); 
     node = next; 
    } 

    return(0); 
} 
+0

ありがとう!私はmallocエラーと "Abort:trap 6"を持っていますが、あなたの応答を今見ています。 – user100000

+0

私は新しい質問[ここ](http://stackoverflow.com/questions/41855178/recursive-function-linked-lists-sprintf-not-saving-variable-into-struct-varia)に尋ねました。見ていただけますか? – user100000

+0

文字列のコピーを作成する必要があります。これは、1)メモリの割り当てと2)コピーを意味します。 ['strdup'](http://en.cppreference.com/w/c/experimental/dynamic/strdup)がこれを行います。 –

0

あなたのプロトタイプを変更して、struct search * paramを追加してリストの先頭にすることができると思います。

あなたのリストに要素を入れる必要があるときは、キューにノードを追加するだけです(単純なポインタの代わりにstruct search **が必要です)。 そして、要素を置く必要があるとき要素を作成し、uはしたい場所に置くつもり関数を呼び出す。その後 Uはちょ​​うどあなたが新しいsearchノードにファイルを見つけるたびに作成する必要があり、すべての再帰呼び出しリストのhead ptr

+0

これを実装しようとします。アイデアをありがとう。 – user100000

関連する問題