实例

爱被打了一巴掌 2023-05-29 13:28 90阅读 0赞

题目要求:设计一个目录扫描程序,输入目录名,扫描该目录下的文件,依次输出扫描的文件名

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <sys/stat.h>
  6. #include <stdlib.h>
  7. void printdir(char *dir,int depth)//depth 控制缩进
  8. {
  9. DIR *dp;
  10. struct dirent *entry;
  11. struct stat statbuf;
  12. if((dp=opendir(dir))==NULL)
  13. {
  14. fprintf(stderr,"Cannot open directory: %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. continue;//忽略.和..目录
  25. printf("%*s%s/\n",depth,"",entry->d_name);
  26. printdir(entry->d_name,depth+4);
  27. }
  28. else//文件
  29. {
  30. printf("%*s%s\n",depth,"",entry->d_name);
  31. }
  32. }
  33. chdir("..");//切换到上层目录
  34. closedir(dp);
  35. }
  36. int main()
  37. {
  38. printf("Directory scan of /home: \n");
  39. printdir("/home/yg/project",0);
  40. printf("Done\n");
  41. return 0;
  42. }

小结:

1.如何将vim 里的代码格式化?

https://blog.csdn.net/weixin_34148340/article/details/94506341

gg=G 将全部代码格式化

nG=mG 将第n行到第m行的代码格式化

注:如果ESC之后输入的是 :gg=G 即前面加了个分号‘:’那么就会有不是编辑器命令的提示。

gg:到达文件最开始,=:要求缩进 ,G:直到文件尾

Linux下DIR,dirent,stat 等结构体详解

  1. struct __dirstream
  2. {
  3. void *__fd;
  4. char *__data;
  5. int __entry_data;
  6. char *__ptr;
  7. int __entry_ptr;
  8. size_t __allocation;
  9. size_t __size;
  10. __libc_lock_define (, __lock)
  11. };
  12. typedef struct __dirstream DIR;

发表评论

表情:
评论列表 (有 0 条评论,90人围观)

还没有评论,来说两句吧...

相关阅读

    相关 实例

    题目要求:设计一个目录扫描程序,输入目录名,扫描该目录下的文件,依次输出扫描的文件名 include <unistd.h> include <stdio.h>