免费注册 查看新帖 |

Chinaunix

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

linux下c语言编程入门例子问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-17 21:46 |只看该作者 |倒序浏览
初学linux c ,今天看到第三章的一个例子:

这个程序有一个参数.如果这个参数是一个文件名,我们输出这个文件的大小和最后修改的时间,如果是一个目录我们输出这个目录下所有文件的大小和修改时间



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <dirent.h>
  7. #include <time.h>

  8. static int get_file_size_time(const char *filename){

  9.         struct stat statbuf;
  10.         if(stat(filename,&statbuf)==-1){
  11.                 printf("Get stat on %s Error: %s \n",filename,strerror(errno));
  12.                 return(-1);
  13.         }
  14.         if(S_ISDIR(statbuf.st_mode))return(1);
  15.         if(S_ISREG(statbuf.st_mode)) printf("%s size: %ld bytes \tmodified at %s",filename,statbuf.st_size,ctime(&statbuf.st_mtime));
  16.         return(0);
  17. }

  18. int main(int argc,char **argv){
  19. DIR *dirp;
  20. struct dirent *direntp;
  21. int stats;
  22. if(argc!=2){
  23.         printf("Usage: %s filename\n\a",argv[0]);
  24.         exit(1);
  25. }
  26. if(((stats=get_file_size_time(argv[1]))==0)||(stats==-1))exit(1);
  27. if((dirp=opendir(argv[1]))==NULL){
  28.         printf("Open Directory %s Error: %s\n",argv[1],strerror(errno));
  29.         exit(1);
  30. }
  31. while((direntp=readdir(dirp))!=NULL)
  32. if(get_file_size_time(direntp-<d_name)==-1)break;
  33. closedir(dirp);
  34. exit(1);
  35. }

复制代码


这个程序在输入文件的时候正常,但输入目录的时候,有时候就会出错




  1. [root@cent52 3.dirfind]# ls
  2. dirf  dirf.c  kk
  3. [root@cent52 3.dirfind]# cd kk
  4. [root@cent52 kk]# ls
  5. dirf  dirf.c
  6. [root@cent52 3.dirfind]# ./dirf kk
  7. dirf size: 6091 bytes   modified at Fri Sep 12 17:29:29 2008
  8. dirf.c size: 958 bytes  modified at Wed Sep 17 21:07:27 2008

复制代码


这样就没有问题,但下面就有问题


  1. [root@cent52 kk]# ls
  2. dirf  dirf.c
  3. [root@cent52 kk]# cp /etc/* .
  4. [root@cent52 kk]# ls
  5. adjtime                         login.defs
  6. aliases                         logrotate.conf
  7. aliases.db                      ltrace.conf
  8. anacrontab                      mailcap
  9. at.deny                         mail.rc
  10. autofs_ldap_auth.conf           man.config
  11. auto.master                     mime.types
  12. auto.misc                       mke2fs.conf
  13. auto.net                        modprobe.conf
  14. auto.smb                        modprobe.conf.BeforeVMwareToolsInstall
  15. bashrc                          motd
  16. .....很多文件省略, 但没有子目录的。
  17. [root@cent52 kk]# cd ..
  18. [root@cent52 3.dirfind]# ls
  19. dirf  dirf.c  kk
  20. [root@cent52 3.dirfind]# ./dirf kk
  21. Get stat on mtools.conf Error: No such file or directory

复制代码


不知道哪里的问题?

论坛徽章:
0
2 [报告]
发表于 2008-09-17 23:07 |只看该作者
你没有改变工作路径(chdir)或者说stat时候没有带入路径。
第一个演示dirf kk之所以能成功。是因为kk/dirf和kk/dirf.c在kk所在的目录下也有。
实际上你stat的结果是和kk同目录下的这两个文件,不是你想要的kk目录下的文件。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP