readdir() beginning with dots instead of files

42,732

. is a directory entry for current directory

.. is a directory entry for the directory one level up in hierarchy

You have to just filter them out using:

if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") )
{
     // do nothing (straight logic)
} else {
     file_name = dp->d_name; // use it
}

More on using . and .. on Windows:

".\\file" - this is a file named file in current working directory

"..\\file" - this is a file in a parent directory

"..\\otherdir\\file" - this is a file that is in directory named otherdir, that is at the same level as current directory (we don't have to know what directory are we in).

Edit: selfcontained example usage of readdir:

#include <stdio.h>
#include <dirent.h>
#include <string.h>

int main()
{
    DIR *dir;
    struct dirent *dp;
    char * file_name;
    dir = opendir(".");
    while ((dp=readdir(dir)) != NULL) {
        printf("debug: %s\n", dp->d_name);
        if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") )
        {
            // do nothing (straight logic)
        } else {
            file_name = dp->d_name; // use it
            printf("file_name: \"%s\"\n",file_name);
        }
    }
    closedir(dir);
    return 0;
}
Share:
42,732
user3036674
Author by

user3036674

Updated on August 01, 2020

Comments

  • user3036674
    user3036674 almost 4 years

    I have a little problem. I'm reading files from directory and it works, but it read two extra files on the beginning ...what is it? for example, there is a list of files: "A348", "A348A", "A348B" and this is what i get: ".", "..", "A348", "A348A", "A348B" ???

    DIR *dir;
    struct dirent *dp;
    char * file_name;
    while ((dp=readdir(dir)) != NULL) {
    
            file_name = dp->d_name;            
    }
    
  • Some programmer dude
    Some programmer dude over 10 years
    The are not only aliases. On e.g. UNIX systems they are actual directory entries in the file system, stored on disk even.
  • user3036674
    user3036674 over 10 years
    @nio: It doesn't work.... but never mind I've already fix it...Don't ask me why but I add dp=readdir(dir); dp=readdir(dir); before while() and it works...
  • unwind
    unwind over 10 years
    @user3036674 That is a really bad idea. nio's code is how to do it properly, if it doesn't work chances are you made some mistake.
  • Art
    Art over 10 years
    @user3036674 That's an awful idea. No standard guarantees that '.' and '..' come first from readdir. They usually do, but sometimes they won't and your program will break in a mysterious way. Do it properly.
  • user3036674
    user3036674 over 10 years
    Ok guys I try nio's idea again.. I know that what I did is dirty as hell... :D thanks for help :)
  • chux - Reinstate Monica
    chux - Reinstate Monica over 10 years
    @Art Example: A top level directory may not a "..".
  • Art
    Art over 10 years
    @chux Actually, they usually do. I'll admit to a dirty secret here. I've implemented filesystems in the past. Our readdir didn't return '.' and '..' as the first two entries and so many applications broke it wasn't even funny. We then decided to just internally special case things in the kernel and return '.' and '..' as the first two entries because the effort of bug reporting every broken application was too much (they essentially did that readdir twice trick). I suspect this is also why root directories fake having a '..', because there is too much broken code out there.