- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个基本程序来汇总当前目录和所有子目录中所有文件的文件大小。我认为这将是一个很好的练习。我正在尝试为此使用基本递归。这是我目前所拥有的:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
long totalbytes = 0;
long dirsize(const char* directory, int verbose)
{
struct dirent *de;
struct stat s;
DIR * dir;
//long total_items = 0;
long filesize = 0;
dir = opendir(directory);
if (dir == NULL)
{
printf("Failed to open %s.\n", directory);
return -1;
}
while ((de = readdir (dir)) != NULL)
{
stat(de->d_name, &s);
if (S_ISLNK(s.st_mode))
{
printf("links are ignored.\n");
}
if (de->d_type == DT_REG)
{
filesize = 0; //be sure to reset this each time to avoid inaccuracy
stat(de->d_name, &s); // get file info into our s structure
if (verbose)
{
printf("%s/%s : %ld bytes (%f MB)\n", directory, de->d_name, s.st_size, (float) s.st_size / 1024 / 1024);
}
filesize = s.st_size; //put file size into filesize variable
totalbytes += filesize; //increment totalbytes
}
if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
{
char pathname[PATH_MAX];
sprintf(pathname, "%s/%s", directory, de->d_name);
dirsize(pathname, verbose); //recursion: keep looping until no more subdirs remain
}
}
closedir(dir);
return totalbytes;
}
long compute_size(const char* directory, int verbose)
{
long space = dirsize(directory, verbose);
return space;
}
int main(int argc, char* argv[])
{
if (argc != 2)
{
printf("Usage: dirsize DIRECTORY\n");
return -1;
}
int verbose = 1; //show or hide individual computations
long space = compute_size(argv[1], verbose);
if (space != -1)
{
float space_mb = (float) space / 1024 / 1024;
printf("space occupied: %ld bytes\n", space);
printf("(%f MB)\n", space_mb);
}
return 0;
}
这是我的程序输出(我还包括子目录的 ls)
08:35 PM@~/tmp$ ./dirsize .
./dirsize : 8369 bytes (0.007981 MB)
./diskstat.c : 1430 bytes (0.001364 MB)
./diskstat : 7993 bytes (0.007623 MB)
./ftw.c : 491 bytes (0.000468 MB)
./a.out : 8044 bytes (0.007671 MB)
./arrays.sh : 212 bytes (0.000202 MB)
./fileread/timestamp : 0 bytes (0.000000 MB)
./fileread/timestamp.c : 0 bytes (0.000000 MB)
./fileread/a.out : 8044 bytes (0.007671 MB)
./fileread/build.prop : 8044 bytes (0.007671 MB)
./fileread/read.c : 4096 bytes (0.003906 MB)
./fileread/read : 454656 bytes (0.433594 MB)
./local.fstab : 76 bytes (0.000072 MB)
./dirsize.c : 1857 bytes (0.001771 MB)
./echo.sh : 223 bytes (0.000213 MB)
./TEAMS.sh : 605 bytes (0.000577 MB)
space occupied: 504140 bytes
(0.480785 MB)
08:35 PM@~/tmp$ ls -l fileread/
total 40
-rwxr-xr-x 1 raidzero raidzero 8132 Dec 28 10:39 a.out
-rw-r--r-- 1 raidzero raidzero 2346 Dec 28 10:09 build.prop
-rwxr-xr-x 1 raidzero raidzero 8384 Dec 29 13:57 read
-rw-r--r-- 1 raidzero raidzero 1150 Dec 28 10:16 read.c
-rwxr-xr-x 1 raidzero raidzero 8132 Dec 29 13:57 timestamp
-rw-r--r-- 1 raidzero raidzero 659 Dec 28 10:39 timestamp.c
08:35 PM@~/tmp$
以我有限的经验,获取随机数据是内存分配不当的标志,但 stat 结构中的 st_size 是一个 long int..?
最佳答案
de->d_name
只有文件名 - 它不包含指向它的路径。例如,您正在统计 ./timestamp
而不是 ./fileread/timestamp
。您可以构建文件的完整路径,使用 chdir()
随时输入目录(如果您处于多线程环境中则不安全,并且如果目录被移动则可能很脆弱当你在其中时 - fchdir()
对回到更高的目录非常有帮助),或者(在最近的 unixen 上)使用 openat()
/fstat()
遍历目录的函数。
请注意,如果您检查过 stat
的错误返回,您会注意到这一点。始终检查那些错误返回。
关于c - 编写简单的 du clone。获取子目录中文件大小的随机值。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8686126/
有没有办法为我的一些文件创建一个子目录?这纯粹是为了文件组织。我有大量的小结构/方法,我想将它们放入它们自己的文件和子目录中,但我不想将它们放入它们自己的包中。他们依赖于我项目中的其他功能。每一个都在
我想将目录中的文件和文件夹复制到另一个文件夹中,但不包含包含该文件的子文件夹,例如,对于node_modules目录,我有大量文件,例如100Mb和50K +个文件,不需要复制。 我试过这样使用xco
嘿,我想安装一个论坛(xenforo),我已经得到了所有的 .php 文件,我把文件夹放在/usr/share/nginx/html 页面在哪里(主页 index.html),但是当我做 127.0.
我想在我的 Symfony2 应用程序的子目录中隔离一些 Controller 。 像这样的东西: route: resource: "@MyBundle/Controller/Admin/"
我们有一个由离岸外包开发公司开发的旧应用程序,它仍在使用 Azure 存储客户端 1.7。 因此,我会在此版本停止工作之前对其进行更新。 有一个单元测试我无法通过。 [TestMethod()
我已将 WordPress 安装在子目录中: /public_html/blog/ 我希望能够像这样访问博客: http://example.com/blog 以及类似这样的帖子: http://ex
我正在尝试制作一个程序来将特定文件夹中的文件以及主文件夹的子文件夹中的文件备份到另一个备份文件夹。 这是我试图实现目标的代码的一部分,但是我只备份了主文件夹中的文件,而子文件夹正在被完全复制(其中的所
我无法在 NSTemporaryDirectory 子文件夹中存储任何文件 rootDirectoryName 是 GUIDsubDirectoryName 也是一个 GUID self.rootFo
我最近正在制作一些 Java 软件来查找文件夹中的一些文件/目录,如果它们的名称包含某些文本,它们将被重命名为其他名称。我使用 Files.walkFileTree 遍历目录,如果找到一个匹配的文件/
我一直在互联网深处搜索,试图让 HAProxy 正常运行,但我不确定它能否完成我想要的。 我试着按照这个:https://www.haproxy.com/blog/howto-write-apache
我正在尝试查找其中包含最多文件的目录。我知道我可以使用以下方法找到文件数: find -maxdepth 5 -type f | wc -l 但这只有在我知道要检查哪个目录时才有用。我想找到包含最多文
我正在尝试按如下方式组织我的项目目录 外壳 |inc/[头文件] |obj/[目标文件] |src/[源文件] |生成文件 |可执行 根文件夹中的所有内容都可以正常编译,但我在修改我的 makefil
当我在谷歌上搜索 yahoo、godaddy 等时,它们会显示子目录,如附图所示。但是当我在谷歌上找到我的网站时,它并没有显示那种东西。问题是什么? 最佳答案 有机 SERP 部分的 Google 附
我有一个名为“myproject”的项目,它由 git 进行版本控制。它有一个名为“data”的子目录,该目录已被 gitignored。 我可以为数据目录“git init”并将其作为单独的 git
我有一个目录位置,如何创建所有目录?例如C:\Match\Upload 将同时创建 Match 和子目录 Upload(如果不存在)。 使用 C# 3.0 谢谢 最佳答案 Directory.Crea
如何在 C++ 中删除包含所有文件/子目录的文件夹(递归删除)? 最佳答案 说真的: system("rm -rf /path/to/directory") 也许更多您正在寻找的东西,但特定于 uni
在我正在处理的当前项目中,有人决定将二进制文件作为源树的一部分 checkin 。二进制文件位于源代码下方的目录中: project/src # Here is the loc
我最近知道了 meteor 私有(private)子目录。根据文档:“私有(private)子目录是服务器代码可以访问但不提供给客户端的任何文件的位置,例如私有(private)数据文件。”一般来说,
我的存储库中有多个项目(子目录)。所有项目都只有一个名为main.cpp的可执行文件,并且它们都使用common语句中的#include文件夹中的库。文件夹结构如下所示: root | ├────co
如何在 C# 中搜索这样的路径: "C:\MyApp\*\日志" 我想获取与该搜索模式匹配的所有目录。 示例结果: C:\MyApp\20171009\日志 C:\MyApp\20171008\日志
我是一名优秀的程序员,十分优秀!