How can I check if a file name is a directory or not in C?
strstr
searches for a substring within another string, so it returns a match for every name that contains one (zero or double) period.
You probably used strcmp
:
if (strcmp(dir->d_name, ".") && strcmp(dir->d_name, ".."))
.. not one of the default root folders ..
Before or after that, you can check if it's not a folder:
if (dir->d_type == DT_DIR)
..
or use stat
. (Note that d_type
may not be supported by some file system types.)
source to share
If you are working on Linux, you can use getdents
that include record type. Otherwise, you probably have to use stat
/lstat
to get the type information for each element.
source to share