c - S_ISDIR 不能过滤是否是目录
问题描述
void my_double_ls(char *dirname)
{
struct dirent *d;
struct stat statbuf;
DIR *dp;
if ((dp = opendir(dirname)) == NULL)
exit(1);
while ((d = readdir(dp)) != NULL)
{
if (d->d_ino != 0)
{
d->d_name[d->d_namlen] = 0;
if (stat(d->d_name, &statbuf) == -1)
{
printf("%s %d %d %d\n", d->d_name, S_ISDIR(statbuf.st_mode), S_ISREG(statbuf.st_mode), d->d_namlen);
}
else
{
if (S_ISDIR(statbuf.st_mode))
{
printf("%s *\n", d->d_name);
}
else
{
printf("%s\n", d->d_name);
}
}
}
}
closedir(dp);
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("wrong argument numbers\n");
exit(1);
}
my_double_ls(argv[1]);
return 0;
}
我是这样写的,但是当我插入文件名并检查stat function
所有 stat 的 d(dirent) 名称的返回值是否为 -1 并且 S_ISDIR 始终为真时,即使存在 txt 文件,这有什么问题?
解决方案
推荐阅读
- sql - 为什么 group by 和 sum 不删除行中的 dup 值
- css - 更少的数组循环会不断附加先前的值
- java - 谷歌地图 - 如何删除地图工具栏
- redirect - 如何使用 nginx-ingress-controller 将 www url 重定向到 no-www url?
- grails - 用户注册后使用弹簧安全休息自动登录
- javascript - 如何关闭鼠标滚轮滚动的被动事件监听器
- mysql - 如何获得正确的最小值和最大值
- ruby-on-rails - 在 simple_form 中间输出
- java - 如何访问json中的数组?
- python - 对python 2中允许但在python 3中不允许的混合变量类型进行排序?