求助,《linux程序设计》 的一段代码
来源:互联网 发布:java ftpclient 文件夹 编辑:程序博客网 时间:2024/05/23 00:37
求助,以下是《linux程序设计》 的一段代码,单步调试时发现一个很奇怪的现象,程序运行到 36 行 : closedir(dp); 再单步运行,程序会跑到 18 行: while((entry=readdir(dp))!=NULL),好像不是很符合c语言的逻辑?
1#include<dirent.h>
2 #include<sys/types.h>
3 #include<string.h>
4 #include<stdio.h>
5 #include<stdlib.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8 void printfdir(char * dir, int depth)
9 { DIR *dp;
10 struct dirent *entry;
11 struct stat statbuf;
12 if((dp=opendir(dir) )==NULL)
13 {
14 fprintf(stderr,"can't open dir%s\n",dir);
15 return;
16 }
17 chdir(dir );
18 while((entry=readdir(dp))!=NULL)
19 {
20 lstat(entry->d_name,&statbuf);
21 if(S_ISDIR(statbuf.st_mode))
22 {
23 if((strcmp(".",entry->d_name)==0)||( strcmp("..",entry->d_name)==0))
24
25
26 continue;
27 printf("%*s%s/\n",depth,"",entry->d_ name);
28
29 printfdir(entry->d_name,depth+4);
30 }
31 else
32
33 printf("%*s%s/\n",depth,"",entry->d_ name);
34 }
35 chdir("..");
36 closedir(dp);
37
}
40 int main()
41 {
42 printf("scan /share/begin_linux:\n");
43 printfdir("/share/begin_linux/",0);
44 exit(0);
45 }
1#include<dirent.h>
2 #include<sys/types.h>
3 #include<string.h>
4 #include<stdio.h>
5 #include<stdlib.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8 void printfdir(char * dir, int depth)
9 { DIR *dp;
10 struct dirent *entry;
11 struct stat statbuf;
12 if((dp=opendir(dir) )==NULL)
13 {
14 fprintf(stderr,"can't open dir%s\n",dir);
15 return;
16 }
17 chdir(dir );
18 while((entry=readdir(dp))!=NULL)
19 {
20 lstat(entry->d_name,&statbuf);
21 if(S_ISDIR(statbuf.st_mode))
22 {
23 if((strcmp(".",entry->d_name)==0)||( strcmp("..",entry->d_name)==0))
24
25
26 continue;
27 printf("%*s%s/\n",depth,"",entry->d_ name);
28
29 printfdir(entry->d_name,depth+4);
30 }
31 else
32
33 printf("%*s%s/\n",depth,"",entry->d_ name);
34 }
35 chdir("..");
36 closedir(dp);
37
}
40 int main()
41 {
42 printf("scan /share/begin_linux:\n");
43 printfdir("/share/begin_linux/",0);
44 exit(0);
45 }
0 0
- 求助,《linux程序设计》 的一段代码
- 求助supesite中的一段代码
- 求助一段关于数组的代码,在线等~~~
- 一段求助的情景对话
- 求助XDJM们,帮忙解释一下一段代码
- 一段linux调度代码的注释
- 一段简单的Linux线程池代码
- linux下测试一段代码的执行时间
- Linux串口操作的一段代码
- 一段不可思议的代码
- 一段写晕掉的代码
- 一段好玩的代码
- 一段有趣的代码
- 一段漏洞百出的代码
- 一段弃暗投明的代码
- treeview的一段代码
- 一段分页的代码
- 一段简单的代码
- Unity3D 多人网络游戏-客户端于服务器的同步
- Servlet 工作原理解析
- MapReduce工作原理图文详解 .
- 最火的android开源项目(一)
- 解决hexo博客的乱码问题
- 求助,《linux程序设计》 的一段代码
- 多线程下写日志
- hdu 4786 Fibonacci Tree
- http://download.csdn.net/detail/kai27ks/8105497
- 最火的android开源项目(二)
- SDWebImage使用——一个可管理远程图片加载的类库
- 关于delete释放指针要注意的问题
- 利用JNI方法,通过WMI获取本地硬件信息(主板型号,硬盘序列号,CPU参数等)
- 第10周项目1 程序填充与阅读