I wrote a small function that receives a path of a folder and should return the number of files inside it.
For some reason my answer is not correct: When printing it I saw that it prints .
and ..
.
Can someone tell me why? Would decreasing 2
solve it?
Here is the code:
{
struct dirent *entry;
DIR *dirp;
int fileCount = 0;
char currPath[MAX_STR];
dirp = opendir(clientLocalPath);
printf(" ***** printing files!!!!! ********\n");
while((entry = readdir(dirp)) != NULL)
{
strcpy(currPath, entry->d_name);
printf("%s\n",currPath);
fileCount++;
}
closedir(dirp);
return fileCount;
}
This is normal and it happens because both
.
and..
are part of the directories:.
refers to the current one and..
to the parent.From What is a dot only named folder?:
From Why do directory listings contain the current (.) and parent (..) directory?: