2016-10-01 3 views
0

Ich versuche, meine verknüpften Liste Konzepte zu putzen. Als ein Beispiel versuche ich eine verknüpfte Liste mit Dateien eines Verzeichnisses in der Reihenfolge der Reihenfolge zu erstellen.verknüpfte Liste von Dateien in einem Verzeichnis mit zuletzt append

struct node *head = NULL; 
    struct node *prev = head; 

    DIR *d; 
    struct dirent *dir; 
    d = opendir("/var/amit12/test1/"); 
    if(d) { 
     while ((dir = readdir(d)) != NULL) { 
      if(dir->d_type == DT_REG) { 
       struct node *last = (struct node*) malloc(sizeof(struct node)); 
       char full_path[18 + strlen(dir->d_name)]; 
       strcpy(full_path, "/var/amit12/test1/"); 
       strcat(full_path, dir->d_name); 
       last->song_id = open(full_path, O_RDONLY); 
       last->name = full_path; 
       last->next = NULL; 
       if(head == NULL) { 
        head = last; 
        prev = last; 
       } else { 
        prev->next = last; 
        prev = last; 
       } 
       //prev.next = &last; 
       //prev = last; 
       printf("%s\n", dir->d_name); 
      } 
     } 
     closedir(d); 
    } 

printf("printing\n"); 

    struct node *root = head; 

    while(root != NULL) { 
     printf("%s\n", root->name); 
     root = root->next; 
    } 

Dies scheint immer mit einem Seg Fehler zu enden.

+3

Zwei Dinge. (1) Sie sind eins nach schüchtern bei der Berechnung der Saitenlänge, und (2) 'last-> name = full_path;' wird * nicht * Gutes verheißen. Jede Iteration der Schleife zerstört diese automatische var 'full_path' und sobald die Schleife beendet ist, haben Sie eine Liste voller ungültiger Name Pointer. Sie müssen eine dynamische * Kopie * des von Ihnen erstellten Namens erstellen. – WhozCraig

+0

@WhozCraig (1) Ahh richtig! (2) Was wäre der richtige Weg, dies zu korrigieren? – Amit

+0

@WhozCraig nvm, dynamische Kopie. Hab das nicht gelesen! – Amit

Antwort

0

Wie WhozCraigh sagen Sie müssen malloc jedes Mal fullpath, oder bei jeder Iteration wird es zerstört werden.

Dies wird die Arbeit machen:

struct node *head = NULL; 
struct node *prev = head; 
char *full_path = NULL; 

DIR *d; 
struct dirent *dir; 
d = opendir("/var/amit12/test1/"); 
if(d) { 
    while ((dir = readdir(d)) != NULL) { 
     if(dir->d_type == DT_REG) { 
      struct node *last = (struct node*) malloc(sizeof(struct node)); 
      full_path = strnew(strlen("/var/amit12/test1/") + strlen(dir->d_name); 
      strcpy(full_path, "/var/amit12/test1/"); 
      strcat(full_path, dir->d_name); 
      last->song_id = open(full_path, O_RDONLY); 
      last->name = full_path; 
      last->next = NULL; 
      if(head == NULL) { 
       head = last; 
       prev = last; 
      } else { 
       prev->next = last; 
       prev = last; 
      } 
      //prev.next = &last; 
      //prev = last; 
      printf("%s\n", dir->d_name); 
     } 
    } 
    closedir(d); 
} 

printf("printing\n"); 

struct node *root = head; 

while(root != NULL) { 
    printf("%s\n", root->name); 
    root = root->next; 
} 
Verwandte Themen