readdir() beginning with dots instead of files [duplicate]
Asked Answered
V

2

11

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;            
}
Varner answered 28/11, 2013 at 11:44 Comment(4)
. is your current directory, .. is one level up.Recreant
Ok, I though that, but I want to get only file names... Any ideas how to do it?Varner
Check for ".." and ".", and simply skip those entries?Halmahera
if (file_name->d_type != DT_DIR ) { // your code } if you want to skip all foldersAurignacian
C
29

. 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;
}
Caravaggio answered 28/11, 2013 at 11:47 Comment(7)
The are not only aliases. On e.g. UNIX systems they are actual directory entries in the file system, stored on disk even.Halmahera
@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...Varner
@Varner 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.Kirschner
@Varner 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.Sophistic
Ok guys I try nio's idea again.. I know that what I did is dirty as hell... :D thanks for help :)Varner
@Sophistic Example: A top level directory may not a "..".Racoon
@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.Sophistic
B
0

Avoid taking the files whose name . and ..

Beverleybeverlie answered 28/11, 2013 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.