.
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;
}
.
is your current directory,..
is one level up. – Recreant".."
and"."
, and simply skip those entries? – Halmaheraif (file_name->d_type != DT_DIR ) { // your code }
if you want to skip all folders – Aurignacian