I'm using if(strstr(dir->d_name, ".") == NULL && strstr(dir->d_name, "..")
to check if its a directory/subdirectory, but this is still printing out some files that are not directories...Im using the direct struct and DIR.
我正在使用if(strstr(dir-> d_name,“。”)== NULL && strstr(dir-> d_name,“..”)来检查它是否是一个目录/子目录,但这仍然是打印出一些文件这不是目录...我使用直接结构和DIR。
3 个解决方案
#1
strstr
searches for a substring inside another string, so it will return a match for every name that contains a single (well, or a double) period.
strstr在另一个字符串中搜索子字符串,因此它将为包含单个(well或double)句点的每个名称返回一个匹配项。
You probably meant to use strcmp
:
你可能打算使用strcmp:
if (strcmp(dir->d_name, ".") && strcmp(dir->d_name, ".."))
.. not one of the default root folders ..
Before or after this, you can check if it is a folder or not:
在此之前或之后,您可以检查它是否是文件夹:
if (dir->d_type == DT_DIR)
..
or use stat
. (Note that d_type
may not be supported by certain file system types.)
或使用stat。 (请注意,某些文件系统类型可能不支持d_type。)
#2
Personally, I like stat() and fstat(). You then look at the st_mode field of the output with macros like S_ISDIR(m).
就个人而言,我喜欢stat()和fstat()。然后使用类似S_ISDIR(m)的宏查看输出的st_mode字段。
#3
If you're on Linux, you can use getdents
which include entry type. Otherwise you'll probably have to use stat
/lstat
to get type info for every item.
如果您使用的是Linux,则可以使用包含条目类型的getdents。否则,您可能必须使用stat / lstat来获取每个项目的类型信息。
#1
strstr
searches for a substring inside another string, so it will return a match for every name that contains a single (well, or a double) period.
strstr在另一个字符串中搜索子字符串,因此它将为包含单个(well或double)句点的每个名称返回一个匹配项。
You probably meant to use strcmp
:
你可能打算使用strcmp:
if (strcmp(dir->d_name, ".") && strcmp(dir->d_name, ".."))
.. not one of the default root folders ..
Before or after this, you can check if it is a folder or not:
在此之前或之后,您可以检查它是否是文件夹:
if (dir->d_type == DT_DIR)
..
or use stat
. (Note that d_type
may not be supported by certain file system types.)
或使用stat。 (请注意,某些文件系统类型可能不支持d_type。)
#2
Personally, I like stat() and fstat(). You then look at the st_mode field of the output with macros like S_ISDIR(m).
就个人而言,我喜欢stat()和fstat()。然后使用类似S_ISDIR(m)的宏查看输出的st_mode字段。
#3
If you're on Linux, you can use getdents
which include entry type. Otherwise you'll probably have to use stat
/lstat
to get type info for every item.
如果您使用的是Linux,则可以使用包含条目类型的getdents。否则,您可能必须使用stat / lstat来获取每个项目的类型信息。