Thread: irectories issue iterating through linked linked?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    irectories issue iterating through linked linked?

    I am coding this program to just save the directory names into a linked list. I THINK have done that successfully (because it prints the strings correctly right after it saves it". My issue now is that my function printData only prints the first element of the list. I guess the "next" element is null. I am not sure why it won't print the whole list. can anyone send me feedback what the issue is iterating through this linked list. I would really appreciate it. Code is below
    Code:
    #include  <sys/types.h>
    #include  <sys/stat.h>
    #include  <dirent.h>
    #include  <ftw.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct path{
    	char *fp;
    	struct fs* filestats;
    	struct path* next;	
    };
    struct filestats{
    	char *fname;
    	int fsize;
    	struct filestats* next;
    };
    	
    void openSubDir(const char *dName);
    void printData(struct path *end);
    int tcount;
    
    struct path *end;
    struct path *start;
    struct path *temp;
    
    int main(int argc, char *argv[]) {
    tcount=0;
      int storeData(const char *, const struct stat *, int);
      
      ftw(argv[1], storeData, 5); 
    	//start = end;
    	printData(start);
    	//system("PAUSE"); 
    	return 0;
     
    } /* End of main. */
    
    int  storeData(const char *name, const struct stat *sbuf, int ftype){
    		
    	end = malloc(sizeof(struct path)); //allocate space for the record pointer
    	memset(end, 0, sizeof(struct path)); //sets up pointer so rec has some space that can be filled up	
    	
    	if(ftype == FTW_D){ //if the object is a directory
    		if(start == NULL){			
    			start = end;
    			start->fp = malloc(strlen(name));
    			strcpy(start->fp, name);				
    			end = start->next;
    			
    		}
    		else{
    			//end->next = NULL;
    			
    			end->fp = malloc(strlen(name));
    			strcpy(end->fp, name);
    			//printf("%s\n", end->fp);			
    			end= end->next;			
    		}		
    	}
    	tcount++;
    	return 0;
    }
    
    void printData(struct path *endF){
    	struct path *temp;
    	temp = endF;
    	while(temp != NULL){
    		if(temp->fp != NULL){
    			printf("%s\n", temp->fp);
    		}
    		temp = temp-> next;
    	}
    }
    Last edited by coolprogrammer; 05-22-2010 at 06:15 PM. Reason: edit:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list issue
    By roaan in forum C Programming
    Replies: 6
    Last Post: 09-01-2009, 01:21 AM
  2. Iterating through a linked list
    By Tom_Arch in forum C Programming
    Replies: 1
    Last Post: 04-24-2009, 08:24 PM
  3. Linked List remove node issue
    By prihod in forum C Programming
    Replies: 1
    Last Post: 04-19-2008, 09:54 AM
  4. Linked list copy constructor issue
    By Craptastic! in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2003, 08:30 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM