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.

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);
Member Avatar for MonsieurPointer

// 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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.