Thread: Explanation Please

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    7

    Question Explanation Please

    Could someone out there explain this code. I have placed "/* explain */ " next to the statements that I have no clue has to what or how they work.

    thanks

    Code:
    #include <stdio.h> 
    #include <dir.h> 
    #include <string.h> 
    #define ALL_ATTS  (FA_DIREC | FA_ARCH) /* explain */ 
    
    void walker(const char *, const char *);
    
    
    int main(void)
    {
      const char *root = "\\";
      char buf[BUFSIZ];
      char * line_ptr;
      
      printf ("buf size: %d\n", sizeof(buf)); 
      printf ("This program will find a file on the current drive.\n"
              "Enter the name of the file to look for: ");
    
      fflush(stdout);  
    
      if (fgets(buf, sizeof(buf), stdin))
      {
        strtok(buf, "\n");
        
        line_ptr = buf;
        printf ("buf: %s\n", line_ptr);
        printf ("buf: %s\n", buf);
        
        walker(root, buf);
      }
      
      return(0);
    }
    
    void walker(const char *path, const char *findme)
    {
      struct ffblk  finder;/* explain */ 
      unsigned int  res;
    
      chdir(path);
    
    /* explain */ 
      for (res = findfirst("*.*", &finder, ALL_ATTS); res == 0; res = findnext(&finder)) /* explain */ 
      {
        if (strcmp(finder.ff_name, ".") == 0) continue;   /* explain */ 
        if (strcmp(finder.ff_name, "..") == 0) continue;  /* explain */ 
    
        if (finder.ff_attrib & FA_DIREC) /* explain */ 
        {
          char newpath[MAXPATH];
          strcpy(newpath, path);
          strcat(newpath, "\\"); 
          strcat(newpath, finder.ff_name);
          chdir(finder.ff_name); /* explain */ 
          walker(newpath, findme);
          chdir(".."); /* explain */ 
        }
        else
        {
          if (strcmp(finder.ff_name, findme) == 0)
          {
            printf("Found in: %s\n", path);
          }
        }
      }
    }

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    102

    Re: Explanation Please

    Code:
    #include <stdio.h> 
    #include <dir.h> 
    #include <string.h> 
    #define ALL_ATTS  (FA_DIREC | FA_ARCH) /* explain */
    basicly he is saying ALL_ATTS is the same as FA_DIREC and FA_ARCH. Like when one does a open() wiht multipule flags

    Code:
    void walker(const char *path, const char *findme)
    {
      struct ffblk  finder;/* explain */
    Im not gonna tell you but look at how a structur is definded

    struct name
    {
    variables
    } tag;


    Code:
      chdir(path);
    
    /* explain */
    man chdir.. the name really is really explaniatory

    Code:
      for (res = findfirst("*.*", &finder, ALL_ATTS); res == 0; res = findnext(&finder)) /* explain */
    Look at for loops, its really strate forward.
    Code:
      {
        if (strcmp(finder.ff_name, ".") == 0) continue;   /* explain */ 
        if (strcmp(finder.ff_name, "..") == 0) continue;  /* explain */
    What are you look for here? they are plain if statements

    Code:
        if (finder.ff_attrib & FA_DIREC) /* explain */ 
        {
          char newpath[MAXPATH];
          strcpy(newpath, path);
          strcat(newpath, "\\"); 
          strcat(newpath, finder.ff_name);
          chdir(finder.ff_name); /* explain */ 
          walker(newpath, findme);
          chdir(".."); /* explain */
    the if(finder.ff_attrib & FA_DIREC) is testing to see if the flag FA_DIREC is set.

    the chdir()'s are explaind in the man pages.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    7
    Thanks Squid,

    I am mostly stuck on:

    What is this, where did it come from and what does it do: ALL_ATTS (FA_DIREC | FA_ARCH)

    I know about structs but where did ffblk come from
    struct ffblk finder

    and what is it and what is the relationship that allows finder to link with
    finder.ff_name
    finder.ff_attrib

    Where did findfirst and findnext come from? I do not see in the code where they are defined.

    I'm lost because I see a lot of variable names that I cannot see where they come from and how they work together.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists
    By Bleu_Cheese in forum C++ Programming
    Replies: 13
    Last Post: 12-21-2007, 09:17 PM
  2. printf function explanation
    By vice in forum C Programming
    Replies: 3
    Last Post: 09-21-2005, 08:35 PM
  3. Explanation
    By fallonides in forum C Programming
    Replies: 3
    Last Post: 03-19-2003, 01:41 PM
  4. basic linked list declaration..need explanation
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-07-2002, 05:55 PM
  5. Explanation!!!
    By MITCHELL in forum Windows Programming
    Replies: 4
    Last Post: 10-25-2001, 01:13 AM