List files in a directory (without folders)

This is a discussion on List files in a directory (without folders) within the Linux Programming forums, part of the Platform Specific Boards category; From the FAQ: This code lists not only files but folders as well Code: #include <dirent.h> #include <stdio.h> DIR *d; ...

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

    List files in a directory (without folders)

    From the FAQ:

    This code lists not only files but folders as well
    Code:
    #include <dirent.h> 
    #include <stdio.h> 
    
    DIR           *d;
    struct dirent *dir;
    d = opendir(".");
    
    if (d)
    {
      while ((dir = readdir(d)))
      {
        printf("%s\n", dir->d_name);
      }
      closedir(d);
    }
    How do I list only files?


    .

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Try this, but I'm not sure what it does with "." and ".." off the top of my head.

    Code:
    #include <dirent.h> 
    #include <stdio.h> 
    # include <sys/types.h>
    # include <sys/mode.h>
    # include <stat.h>
    
    DIR           *d;
    struct dirent *dir;
    struct stat s;
    d = opendir(".");
    
    if (d)
    {
      while ((dir = readdir(d)))
      {
        if (stat(dir->d_name,&s) != 0) {
            /* is this a regular file? */
            if ((s.st_mode & S_IFMT) == S_IFREG)
                printf("%s\n", dir->d_name);
    
        }
      }
      closedir(d);
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    Thanks but I don't have mode.h on my system (Ubuntu 5.10).

    Quote Originally Posted by IfYouSaySo
    Try this, but I'm not sure what it does with "." and ".." off the top of my head.

    Code:
    #include <dirent.h> 
    #include <stdio.h> 
    # include <sys/types.h>
    # include <sys/mode.h>
    # include <stat.h>
    
    DIR           *d;
    struct dirent *dir;
    struct stat s;
    d = opendir(".");
    
    if (d)
    {
      while ((dir = readdir(d)))
      {
        if (stat(dir->d_name,&s) != 0) {
            /* is this a regular file? */
            if ((s.st_mode & S_IFMT) == S_IFREG)
                printf("%s\n", dir->d_name);
    
        }
      }
      closedir(d);
    }

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Just remove the line "#include <sys/mode.h>" and see if it compiles. Probably will.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    The above code didn't work but I figured it out. Use the following

    if( dir->d_type == DT_DIR )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. list size of files in directory
    By mayhem in forum C Programming
    Replies: 3
    Last Post: 02-27-2005, 11:52 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. Printing list of .txt files saved in directory
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-16-2002, 12:33 AM

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