Reading a directory --code from FAQ

This is a discussion on Reading a directory --code from FAQ within the C Programming forums, part of the General Programming Boards category; hello everybody I had a question which i found an answear to on the FAQ however C is my first ...

  1. #1
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    Unhappy Reading a directory --code from FAQ

    hello everybody

    I had a question which i found an answear to on the
    FAQ however C is my first programming language and i
    dont really understand it all. Can somebody explain
    a few things please.

    This is the code i found;

    Code:
    
    /*
     * This program displays the names of all files in the current directory.
     */
    
    
    
    
    #include <dirent.h> 
    #include <stdio.h> 
    
    int main(void)
    {
      DIR           *d;                   /*what does this mean?*/
      struct dirent *dir;                  
      d = opendir(".");                      /* and this*/
      if (d)
      {
        while ((dir = readdir(d)) != NULL)
        {
          printf("%s\n", dir->d_name);             /* and this (dir->d_name)*/
        }
    
        closedir(d);
      }
    
      return(0);
    }
    I guess i just wanna understand how the code works and how i can chage the directory
    it looks into.

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    DIR *d; /*what does this mean?*/
    A pointer to a DIR structure. It contains information about the current directory entry which is later accessed
    printf("%s\n", dir->d_name); /* and this (dir->d_name)*/
    here.
    I guess i just wanna understand how the code works and how i can chage the directory
    it looks into.
    It's this line:
    d = opendir("."); /* and this*/
    "." is the current directory. You can change this to an absolute or relative path to your liking.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Have a look at how struct's, struct & pointer's works in C and come back to DIR .

    ssharish2005

  4. #4
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Quote Originally Posted by ssharish2005
    Have a look at how struct's, struct & pointer's works in C and come back to DIR .

    ssharish2005
    He may know what they are, he just doesn't know what their purpose is, seeing as how he commented the typedef'd DIR instead of the struct.
    Last edited by divineleft; 11-11-2006 at 03:32 PM.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    DIR           *d;                   /*what does this mean?*/
    It's just so clear that is a pointer variable of some Struct doesn't matter it is typedef struct DIR
    Code:
     printf("%s\n", dir->d_name);             /* and this (dir->d_name)*/
    And this ???????
    Code:
    code from FAQ 


    ssharish2005
    Last edited by ssharish2005; 11-11-2006 at 02:21 PM.

  6. #6
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51
    Hey! C'mon ssharish2005

    If you need an ego boost do something clever instead of picking on noobs
    for your thrills!

    If you aint got nothing constructive to add why bother!!



    Oh, and thanks to OnionKnight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. view network directory code
    By not_someguy in forum C++ Programming
    Replies: 2
    Last Post: 06-25-2008, 02:54 PM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 02:30 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  5. Directory reading!
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 03-10-2002, 09:47 AM

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