给出指定目录文件系统的目录结构,以凹入法写入文件dir_structure.txt文档中,并计算出该目录所占存储区域大小。(凹入法显示参见P93)

本人提供Linux和WindowsC++编译环境下的两个版本的代码,分别可以在Dev-C++和VS2019上取得编译通过!

实现效果:

Dev-C++代码:

#include<stdio.h>
#include<cstring>
#include<unistd.h>
#include<dirent.h>//目录操作的头文件
#include<sys/stat.h>
#include<stdlib.h>
FILE*fp;
//测试模块
long long Size=0l;
void printdir(char *dir, int depth)
{DIR *pdir = opendir(dir);//返回一个目录流if(NULL == pdir){fprintf(stderr,"cannot open directory:%s\n",dir);return;//stderr直接输出,不经缓冲区 }chdir(dir);//用于改变当前工作目录,其参数为Path 目标目录 struct dirent *pentry;struct stat statbuf;while((pentry = readdir(pdir)) != NULL){  stat(pentry->d_name,&statbuf);if(S_ISDIR(statbuf.st_mode)){if(strcmp(".",pentry->d_name) == 0 || strcmp("..",pentry->d_name) == 0){continue;}printf("%*s%s/\n",depth,"",pentry->d_name);fprintf(fp,"%*s%s/\n",depth,"",pentry->d_name);printdir(pentry->d_name,depth+4);}else{// printf("%d",depth);Size+=statbuf.st_size;printf("%*s%s\n",depth,"",pentry->d_name);fprintf(fp,"%*s%s\n",depth,"",pentry->d_name);}}chdir("..");//返回上一级目录closedir(pdir);//关闭参数dir所指的目录流。关闭成功则返回0,失败返回-1
}
int main(){char topdir[]="D:\\Program Files (x86)\\TTKN\\CAJViewer 7.2";fp=fopen("C:\\Users\\wyx03\\Desktop\\dir_structure.txt","w");printf("The directory is %s: \n",topdir);printdir(topdir,0);fclose(fp);printf("Done!\n");printf("The total size :%lld bytes",Size);return 0;
}

VS2019版本实现效果:

VS2019实现代码:

#include<iostream>
#include<string>
#include<io.h>
#include<cstdlib>
#include<cstring>
using namespace std;
FILE* fp;
long long Size=0;//预先设定大小变量
void TransFile(char * Fname, int depth) {fprintf(fp, "%*s%s/\n", depth, "", Fname);
}
void fileSearch(string path, int depth)
{long fHandle = 0;//_findfirst函数返回回来的句柄。/*_finddata_t  存储文件各种信息的结构体,<io.h>;*/struct _finddata_t fa;string pathName;pathName = path;/*\\* 表示符合的所有文件;没有找到即文件夹为空,退出;assign 表示把 pathName清空并置为path;append 表示在末尾加上字符串;c_str 返回一个const char* 的临时指针;_findfirst搜索与指定的文件名称匹配的第一个实例,若成功则返回第一个实例的句柄,否则返回-1L;函数原型:long _findfirst( char *filespec, struct _finddata_t *fa );*/if ((fHandle = _findfirst((pathName+"\\*").c_str(), &fa)) == -1)return;do {if (strcmp(".", fa.name) == 0 || strcmp("..", fa.name) == 0) {continue;//直接在while开始读取下一文件。跳过.和.. }printf("%*s/",  depth, "");string a;a = path + "\\" + fa.name;Size+= fa.size;TransFile(fa.name, depth);//fprintf(fp, "%*s%s/\n", depth, "", fa.name);//cout << path+"\\"+fa.name << endl;cout << fa.name << endl;/*文件夹下有 . 和 .. 目录,不能进入搜索;_A_SUBDIR 表示文件夹属性;*/if (strcmp(fa.name, "..") && strcmp(fa.name, ".") && fa.attrib == _A_SUBDIR)fileSearch(path + "\\" + fa.name, depth + 4);} while (_findnext(fHandle, &fa) == 0);/*_findnext 搜索与_findfirst函数提供的文件名称匹配的下一个实例,若成功则返回0,否则返回-1 ;_findclose 结束查找;*/_findclose(fHandle);return;
}
int main()
{fopen_s(&fp,"C:\\Users\\wyx03\\Desktop\\dir_structure.txt", "w");string path = "C:\\Program Files (x86)\\Microsoft SDKs\\ClickOnce Bootstrapper";fileSearch(path, 0);fclose(fp);cout << "The Size of " + path + " :" << Size << " bytes " << endl;printf("Done!\n");return 0;
}

当然,大家如果希望可以在Dev-C++上运行VS2019的代码,可以试试下面的代码:

#include<iostream>
#include<string>
#include<io.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<fstream>
using namespace std;
FILE* fp;
long long Size=0;
void fileSearch(string path, int depth)
{long hFile = 0;/*_finddata_t  存储文件各种信息的结构体,<io.h>;*/struct _finddata_t fileInfo;string pathName;/*\\* 表示符合的所有文件;没有找到即文件夹为空,退出;assign 表示把 pathName清空并置为path;append 表示在末尾加上字符串;c_str 返回一个const char* 的临时指针;_findfirst搜索与指定的文件名称匹配的第一个实例,若成功则返回第一个实例的句柄,否则返回-1L;函数原型:long _findfirst( char *filespec, struct _finddata_t *fileinfo );*/if ((hFile = _findfirst(pathName.assign(path).append("\\*").c_str(), &fileInfo)) == -1)return;do {if (strcmp(".", fileInfo.name) == 0 || strcmp("..", fileInfo.name) == 0) {continue;//直接在while开始读取下一文件。跳过.和.. }printf("%d%*s/", depth, depth, "");string a;a = path + "\\" + fileInfo.name;Size+= fileInfo.size;fprintf(fp, "%*s%s/\n", depth, "", a.c_str());//cout << path+"\\"+fileInfo.name << endl;cout << fileInfo.name << endl;/*文件夹下有 . 和 .. 目录,不能进入搜索;_A_SUBDIR 表示文件夹属性;*/if (strcmp(fileInfo.name, "..") && strcmp(fileInfo.name, ".") && fileInfo.attrib == _A_SUBDIR)fileSearch(path + "\\" + fileInfo.name, depth + 4);} while (_findnext(hFile, &fileInfo) == 0);/*_findnext 搜索与_findfirst函数提供的文件名称匹配的下一个实例,若成功则返回0,否则返回-1 ;_findclose 结束查找;*/_findclose(hFile);return;
}
int main()
{fp=fopen("C:\\Users\\wyx03\\Desktop\\dir_structure.txt", "w");string path = "C:\\Program Files (x86)\\Microsoft SDKs\\ClickOnce Bootstrapper";fileSearch(path, 0);fclose(fp);cout << "The Size of "+path+" :"<<Size << endl;printf("Done!\n");return 0;
}

对了,引用的时候大家记得把文件路径改一下。。。

凹入法写入指定目录至文档,并计算目录大小相关推荐

  1. 泛微ecology 通过建模引擎导航树展示指定的知识目录展示文档应用场景

    通过建模引擎导航树展示指定的知识目录展示文档应用场景 实现的效果见下图: 第一部分:需求说明 客户在系统使用过程中,针对公告信息想独立显示,公告目录下的文档信息,不需要从整个的知识库中区点击公告目录查 ...

  2. 使用 goodsync 软件将指定目录的文档单向同步到 hexo 博客

    使用 goodsync 软件将指定目录的文档单向同步到 hexo 博客 我的所有笔记保存在 "我的文档" 目录, 我的笔记类型有 .md, .pdf, .docx 等等, 包括私人 ...

  3. VB判断指定的WORD文档是否被打开

    判断指定的WORD文档是否被打开的函数代码如下 Function WordDocIsOpen(ByVal strDocName As String) As Boolean Dim objWordApp ...

  4. Word开发工具Aspose.Words功能演示:使用C ++在Word文档中使用目录

    目录(TOC)是Word文档的重要组成部分.它提供了文档内容的概述,并允许您快速导航到所需的部分.您可能会遇到需要以编程方式从Word文档中添加,提取,更新或删除目录的情况.为此,本文将教您如何使用C ...

  5. 爬虫准备工作1-Java写入字符串到txt文档

    最近想做一个爬虫的功能,在最开始的时候是从一个网页上面获取上面的所有的url,然后将url写入到一个txt文档,那么现在要实现的功能是之前写入txt文档,可能自己没有发现比较简单的方法的吧,所有总是感 ...

  6. DOCTYPE html PUBLIC 指定了 HTML 文档遵循的文档类型定义

    DOCTYPE html PUBLIC 指定了 HTML 文档遵循的文档类型定义 今天看到一篇CSS应用的一个友好搜索,我按网页上的代码复制.粘贴后预览时总达不到效果,而直接拷贝他的实例却能达到效果, ...

  7. word根据目录切块php,PHP导出Word文档如何自定义目录?

    PHP导出Word文档如何自定义目录? 2018-09-19 class word { function start() { ob_start(); } function save($path) { ...

  8. word文档添加多级目录,自动生成目录

    word文档添加多级目录 转载:http://jingyan.baidu.com/article/8ebacdf0c32c5f49f65cd53f.html Word如何添加一级二级标题等多级标题 有 ...

  9. Word 2010文档自动生成目录和某页插入页码

    一.Word 2010文档自动生成目录 关于Word文档自动生成目录一直是我身边同学们最为难的地方,尤其是毕业论文,经常因为目录问题,被要求修改,而且每次修改完正文后,目录的内容和页码可能都会发生变化 ...

最新文章

  1. css图片过大,CSS解决图片过大撑破DIV的方法
  2. 怎么用PHP语句做出增改删查功能,PHP、MYSQLI实现简单的增、删、改、查功能(初学者)...
  3. 猜数大小编程c语言_猜数正确编程
  4. path manipulation怎么解决_干货!终于!解决macOS下pyenv安装python3.8.2缺少tkinter模块的问题!...
  5. 用JS实现的常见几种排序算法
  6. 手机安装证书时候弹出输入证书密码
  7. 【数学】线性代数技巧篇
  8. SQL日志文件损坏或丢失造成数据库置疑的解决办法
  9. namp 端口扫描技术
  10. Python 针对Excel操作
  11. java poi框架导出excel如何插入特殊字符(复选框勾选)
  12. C语言伽罗华域乘法,伽罗瓦域上的乘法
  13. python语言常用的中文分词第三方库是_基于boost使用Python调用NLPIR(ICTCLAS2013)中文分词组件...
  14. Linux-dd命令详解
  15. I2C中关于ACK和NACK的几点东西
  16. Windows11 笔记本有线连接 WLAN(WIFI)共享方法(不知道Win10这种方法好不好使)
  17. 命名空间 namespace
  18. vue中给字段base 64加密
  19. 机器学习理论基础学习10--- 高斯混合模型GMM
  20. 怎么把ppt文件转换成pdf?解决方法有这几种

热门文章

  1. 输入年份和月份,求该月有多少天
  2. 2021年深圳市专精特新中小企业申报要求及遴选内容指南,包括补贴20万
  3. Jupyter notebook中的Markdown语法:表格
  4. 本地摄像头应用到远程计算机,3个可在iPad / iPhone上远程查看网络摄像头的应用 | MOS86...
  5. 唐伯虎的诗,可能是改编的
  6. Qt 关于去除虚线框的三种方法
  7. 《东周列国志》第一回 周宣王闻谣轻杀 杜大夫化厉鸣冤
  8. 写出下列程序运行结果
  9. 网页上的内容无法选中复制该如何解决?
  10. @Enumerated的使用