Thread: recursion with segmentation fault

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    recursion with segmentation fault

    To the recursion part, i got a segmentation fault. Can some one help me out?
    Code:
    int main(argc,argv)
          int argc;
          char **argv;
          {
              DIR *dhandle;
              struct dirent *entry3;
              unsigned char done = 0;
              char path[255];
        
              if (argc !=2)   sprintf(path,".");
              else            strcpy(path,argv[1]);
        
             dhandle=opendir(path);
                  for (entry3=readdir(dhandle);entry3!=NULL;entry3=readdir(dhandle))
                    printfDetails(entry3->d_name);
              rewinddir(dhandle);
              recursive(dhandle);
             closedir(dhandle);
          }
        
          recursive(d)
         DIR *d; {
          
          int exists;
          struct stat statbuf;
          struct dirent  *entry;
        
          for (entry=readdir(d);entry!=NULL;)
             {                                                      
               exists=stat(entry->d_name,&statbuf);
               if ((exists>=0)&&(S_ISREG(statbuf.st_mode)))
                   printf("%20s\n",entry->d_name);
               else if ((exists>=0)&&(S_ISDIR(statbuf.st_mode))&&(entry->d_name[0]!='.')
    )
                  {
                    d=opendir(entry->d_name);
                   recursive (d);
                  }
          else /* bypass the hidden files and . and .. */;
          }
          }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Rough guess, because your code is horribly formatted, and its hard to tell what's going on.
    But your for loop in main() is only doing one statement


    > int main(argc,argv)
    > int argc;
    > char **argv;
    Get a better compiler, or a better book.
    This style of C became out-dated 15 years ago.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    You've also got some issues with your brackets. You need to include all of your main () stuff in brackets. ex
    Code:
    int main()
    {
          blah blah blah
          return(0);
    }
    Not to mention, that like Salem said, this code is increadibly hard to read due to the format you wrote it in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault using recursion to find factorial
    By kapok in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2009, 11:10 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM