Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

readdir

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;            
}
like image 344
user3036674 Avatar asked Nov 28 '13 11:11

user3036674


People also ask

How does Readdir work in C?

The readdir() function returns a pointer to a structure representing the directory entry at the current position in the directory stream specified by the argument dirp, and positions the directory stream at the next entry. It returns a null pointer upon reaching the end of the directory stream.

Is Readdir alphabetical?

The readdir method doesn't guarantee any ordering. If you want to ensure they are sorted alphabetically you'll need to do so yourself. It's by no means definitive but it does give a nice overview of the command, its history and how its implementation is typically traversal order.

What is C++ Readdir?

General description. Returns a pointer to a dirent structure describing the next directory entry in the directory stream associated with dir. A call to readdir() overwrites data produced by a previous call to readdir() or __readdir2() on the same directory stream.

What does Opendir return in C?

Returned value. If successful, opendir() returns a pointer to a DIR object. This object describes the directory and is used in subsequent operations on the directory, in the same way that FILE objects are used in file I/O operations.


1 Answers

. 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;
}
like image 177
nio Avatar answered Sep 18 '22 21:09

nio