免费注册 查看新帖 |

Chinaunix

广告
  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1223 | 回复: 1
打印 上一主题 下一主题

一个linux下的c程序,大家看看为啥结果不正确 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-19 13:08 |只看该作者 |倒序浏览
是书上的习题,要求列出一个目录下的文件和子目录,当skip变量为1时,跳过以.开头的文件,如果是目录,在目录名后打印一个星花。
运行结果很奇怪,是skip为1时,所有文件和目录都不打印星花,skip为其他值时都打印星花,大家看看怎么回事。


#include<dirent.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
int my_double_ls(const char *name,int skip);
main(int argc,char *argv[])
{
my_double_ls(argv[1],atoi(argv[2]));
}
my_double_ls(const char *name,int skip)
{
struct dirent *d;
struct stat s;
DIR *dp;
if((dp=opendir(name))==NULL)
return(-1);
while(d=readdir(dp))
{
if(d->d_ino!=0)
if(skip==1 && d->d_name[0]=='.')
continue;
else
{
stat(d->d_name,&s);
if(S_ISDIR(s.st_mode))
printf("%s *\n",d->d_name);
else
printf("%s \n",d->d_name);
}
}

return(0);
}

论坛徽章:
0
2 [报告]
发表于 2007-04-19 13:10 |只看该作者
处女帖的沙发
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP