0

Please refer to the code below.It is used to extract files from a given directory, for further processing.

include <dirent.h>

include <stdio.h>

include <string.h>

int main(void)
{
int i=0;
struct dirent *direntry;
dir = opendir("E:/muzikk");
if(!dir)
{
printf("Error: directory did not open!\n");
return 1;
}
while((direntry=readdir(dir))!=NULL)
{

        printf("%d %s\n",i,direntry->d_name);
        i++;
}

closedir(dir);
    return 0;

}

The problem is,that on running, it returns the first two file names as '.'(on zero position) and '..'(on first position), which is undesirable. can anyone explain or help. thanks.

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by nee_88
1

That will always happen. '.' is the current directory and '..' is the parent directory.

while((direntry=readdir(dir))!=NULL)
{
    // If the first character is a dot, then skip it
    if ( direntry->d_name[0] == '.' )
       continue;
    printf("%d %s\n",i,direntry->d_name);
    i++;
}
closedir(dir);
0

// If the first character is a dot, then skip it

That will work, but not 100% of the time. It would fail if you had a directory named .svn, for example.

0

ya..but thats alright for me since the directory contains only text files.Thanks.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.