FAQ help

This is a discussion on FAQ help within the C++ Programming forums, part of the General Programming Boards category; inthe faq there is this code Code: /* * This program displays the names of all files in the current ...

  1. #1
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227

    FAQ help

    inthe faq there is this code

    Code:
    /*
     * This program displays the names of all files in the current directory.
     */
    
    #include <dirent.h> 
    #include <stdio.h> 
    
    int main(void)
    {
      DIR           *d;
      struct dirent *dir;
      d = opendir(".");
      if (d)
      {
        while ((dir = readdir(d)) != NULL)
        {
          printf("%s\n", dir->d_name);
        }
    
        closedir(d);
      }
    
      return(0);
    }
    could someone please explain this code to me, as I would like to use it and manipulate it, but I cant without understanding it first, and i cant seem to figure out how this whole "dir" thing works - ty
    Keyboard Not Found! Press any key to continue. . .

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    Where is the rest of the code?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sand_man
    Where is the rest of the code?
    My guess is, if there is more code, it's in the FAQ.

    As to what it does, I'd venture the following guess:
    /*
    * This program displays the names of all files in the current directory.
    */
    If that isn't good enough, you could always oh, I don't know, google the function. Or man it perhaps...

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    > d = opendir(".");
    Think fopen() for files

    > while ((dir = readdir(d)) != NULL)
    Think fgets() for files

    > closedir(d);
    Think fclose() for files
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Quote Originally Posted by Salem
    > d = opendir(".");
    Think fopen() for files

    > while ((dir = readdir(d)) != NULL)
    Think fgets() for files

    > closedir(d);
    Think fclose() for files
    Granted, the Unix convention is that everything is a file...but I think you meant "for directories".
    My best code is written with the delete key.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Actually, I've no idea what I meant

    I was trying to suggest that if you know what fopen does for a file, then it isn't too far from understanding what opendir does for a directory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Actually, I've no idea what I meant
    That happens to me all the time.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 02:17 PM
  2. FAQ Check/Lock
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-15-2002, 12:21 PM
  3. FAQ - it is not a true list of FAQ
    By alpha561 in forum C Programming
    Replies: 1
    Last Post: 05-25-2002, 07:40 PM

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