Issue with FAQ example

This is a discussion on Issue with FAQ example within the C Programming forums, part of the General Programming Boards category; I'm having some trouble with one of the FAQ examples http://faq.cprogramming.com/cgi-bin/...&id=1044780608 I have adapted the code as below yet all ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    62

    Issue with FAQ example

    I'm having some trouble with one of the FAQ examples

    http://faq.cprogramming.com/cgi-bin/...&id=1044780608

    I have adapted the code as below yet all I get is directory tree with 3 sub folders searched over 1000 times and still not finding the file!

    Code:
    #include <dirent.h> 
    #include <sys/types.h> 
    #include <sys/param.h> 
    #include <sys/stat.h> 
    #include <unistd.h> 
    #include <stdio.h> 
    #include <string.h> 
    int dn = 0;
    
    
    int walker( char *searching, char *result ) {
      DIR           *d;
      struct dirent *dir;
      d = opendir( "/home/s/Pictures" );
      if( d == NULL ) {
        return 1;
      }
      while( ( dir = readdir( d ) ) ) {
        if( strcmp( dir->d_name, "." ) == 0 || 
            strcmp( dir->d_name, ".." ) == 0 ) {
          continue;
        }
        if( dir->d_type == DT_DIR ) {
          chdir( dir->d_name );
    		dn++;
    		printf("Searching......%s\n",dir->d_name);
    		printf("Directory number %i\n",dn);
          walker( searching, result );
          chdir( ".." );
        } else {
          if( strcmp( dir->d_name, searching ) == 0 ) {
            int  len;
            getcwd( result, MAXPATHLEN );
            len = strlen( result );
            snprintf( result + len, MAXPATHLEN - len, "/%s", dir->d_name );
            break;
          }
        }
      }
      closedir( d );
      return *result == 0;
    }
    
    int main( ) {
    	
      char buf[MAXPATHLEN] = { 0 };
      if( walker( "Fenestra.png", buf ) == 0 ) {
        printf( "Found: %s\n", buf );
      } else {
        puts( "Not found" );
      }
      return 0;
    }

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You edited a recursive walker function that traverses the tree into something that can't possibly work, since you put the directory hardcored inside the function:

    Code:
    d = opendir( "/home/s/Pictures" );
    Each time this function is called, it will open that directory. To search inside a specific location, chdir() to that direction before calling walker inside your main.

    Code:
    int main( ) {
      char buf[MAXPATHLEN] = { 0 };
      chdir("/home/s/Pictures");
      if( walker( "Fenestra.png", buf ) == 0 ) {
        printf( "Found: &#37;s\n", buf );
      } else {
        puts( "Not found" );
      }
      return 0;
    }
    and leave the original function alone.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    Thanks a lot. That did it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 01:17 PM
  3. FAQ Check/Lock
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-15-2002, 11:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21