linux C程序实现文件夹大小提取
2011-10-28 23:25
246 查看
在嵌入式linux系统中,经常要对一些实时数据进行存储,而在存储空间有限的情况下往往需要判断存储目录中的文件夹的大小,而通过C语言实现文件夹大小的获取在网上的程序可是少之又少,现提供一个程序,大家一起分享,分享,其实程序是提取文件夹下所有文件大小,提取运行程序文件夹下的文件的大小之和,但不包括文件夹目录下的文件夹的大小。具体程序如下:
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
main()
{
DIR *d;
struct dirent *de;
struct stat buf;
int exists;
int total_size;
d = opendir(".");
if (d == NULL) {
perror("prsize");
exit(1);
}
total_size = 0;
for (de = readdir(d); de != NULL; de = readdir(d)) {
exists = stat(de->d_name, &buf);
if (exists < 0) {
fprintf(stderr, "Couldn't stat %s\n", de->d_name);
} else {
total_size += buf.st_size;
}
}
closedir(d);
printf("%d\n", total_size);
}
以下为另外一个文件夹大小提取程序,程序内容:
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
static unsigned int total = 0;
int sum(const char *fpath, const struct stat *sb, int typeflag)
{
total += sb->st_size;
return 0;
}
int main(int argc, char **argv)
{
if (!argv[1] || access(argv[1], R_OK)) {
return 1;
}
if (ftw(argv[1], &sum, 1)) {
perror("ftw");
return 2;
}
printf("%s: %u\n", argv[1], total);
return 0;
}
通过GCC编译程序
gcc -o dir_size dir_size.c
运行程序
./dir_size /licy/
下面的程序使用statfs函数实现硬盘大小数据提取,及剩余空间大小的提取,并把硬盘大小及剩余空间打印出来。
#include <stdio.h>;
#include <sys/vfs.h>;
#include <error.h>;
#define Gsize (1024.00*1024.00*1024.00)
#define Msize (1024.00*1024.00)
#ifndef EXT2_SUPER_MAGIC
#define EXT2_SUPER_MAGIC 0xef53
#endif
int main()
{
long long blocks,bfree;
struct statfs fs;
if(statfs("/",&fs)<0)
{
perror("statfs");
exit(0);
}
printf("%x\n",fs.f_type); /* type of filesystem (see below) */
printf("%ld\n",fs.f_bsize); /* optimal transfer block size */
printf("%ld\n",fs.f_blocks); /* total data blocks in file system */
printf("%ld\n",fs.f_bfree); /* free blocks in fs */
printf("%ld\n",fs.f_bavail); /* free blocks avail to non-superuser */
printf("%ld\n",fs.f_files); /* total file nodes in file system */
printf("%ld\n",fs.f_ffree); /* free file nodes in fs */
printf("%d\n",fs.f_fsid); /* file system id */
printf("%ld\n",fs.f_namelen); /* maximum length of filenames */
blocks=fs.f_blocks;
bfree=fs.f_bfree;
printf(" %lld\n",blocks);
if(fs.f_type==EXT2_SUPER_MAGIC)
{
printf("Total size of / is %f G\n",blocks*fs.f_bsize/Gsize);
printf("Free size of / is %f G\n",bfree*fs.f_bsize/Gsize);
}
}
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
main()
{
DIR *d;
struct dirent *de;
struct stat buf;
int exists;
int total_size;
d = opendir(".");
if (d == NULL) {
perror("prsize");
exit(1);
}
total_size = 0;
for (de = readdir(d); de != NULL; de = readdir(d)) {
exists = stat(de->d_name, &buf);
if (exists < 0) {
fprintf(stderr, "Couldn't stat %s\n", de->d_name);
} else {
total_size += buf.st_size;
}
}
closedir(d);
printf("%d\n", total_size);
}
以下为另外一个文件夹大小提取程序,程序内容:
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
static unsigned int total = 0;
int sum(const char *fpath, const struct stat *sb, int typeflag)
{
total += sb->st_size;
return 0;
}
int main(int argc, char **argv)
{
if (!argv[1] || access(argv[1], R_OK)) {
return 1;
}
if (ftw(argv[1], &sum, 1)) {
perror("ftw");
return 2;
}
printf("%s: %u\n", argv[1], total);
return 0;
}
通过GCC编译程序
gcc -o dir_size dir_size.c
运行程序
./dir_size /licy/
下面的程序使用statfs函数实现硬盘大小数据提取,及剩余空间大小的提取,并把硬盘大小及剩余空间打印出来。
#include <stdio.h>;
#include <sys/vfs.h>;
#include <error.h>;
#define Gsize (1024.00*1024.00*1024.00)
#define Msize (1024.00*1024.00)
#ifndef EXT2_SUPER_MAGIC
#define EXT2_SUPER_MAGIC 0xef53
#endif
int main()
{
long long blocks,bfree;
struct statfs fs;
if(statfs("/",&fs)<0)
{
perror("statfs");
exit(0);
}
printf("%x\n",fs.f_type); /* type of filesystem (see below) */
printf("%ld\n",fs.f_bsize); /* optimal transfer block size */
printf("%ld\n",fs.f_blocks); /* total data blocks in file system */
printf("%ld\n",fs.f_bfree); /* free blocks in fs */
printf("%ld\n",fs.f_bavail); /* free blocks avail to non-superuser */
printf("%ld\n",fs.f_files); /* total file nodes in file system */
printf("%ld\n",fs.f_ffree); /* free file nodes in fs */
printf("%d\n",fs.f_fsid); /* file system id */
printf("%ld\n",fs.f_namelen); /* maximum length of filenames */
blocks=fs.f_blocks;
bfree=fs.f_bfree;
printf(" %lld\n",blocks);
if(fs.f_type==EXT2_SUPER_MAGIC)
{
printf("Total size of / is %f G\n",blocks*fs.f_bsize/Gsize);
printf("Free size of / is %f G\n",bfree*fs.f_bsize/Gsize);
}
}
相关文章推荐
- Linux C程序实现查看文件夹大小
- linux C程序实现文件夹大小提取
- linux c实现的提取文件名的小程序
- 根据pix飞控log文件和photo文件夹建立pos文件,提取pos数据小程序
- PHP做视频网站,让程序自动实现视频格式转换、设置视频大小、生成视频缩略图
- Python实现比较扑克牌大小程序代码示例
- 用单选按钮组实现选择字体大小的程序
- 用C#实现获取文件夹大小的源代码
- 简易文件夹打包程序的设计与实现
- java程序实现“深度复制文件夹"
- PHP 文件夹操作「复制、删除、查看大小」迭代实现
- 面试题-程序题-Java递归实现遍历文件目录(所有文件夹和子文件)
- 批处理bat文件实现复制、删除、创建文件夹、执行程序、打开文件
- C#递归实现显示文件夹及所有文件并计算其大小的方法
- 黑马程序员 用OC实现的一个计算文件夹下代码行数的小程序
- Java 小程序 实现网络下载图片到制定文件夹,压缩和删除文件
- C# 实现获取文件夹大小的源代码
- C# ZIP压缩工具小程序实现 支持多个文件和文件夹
- 如何在程序中,实现比大小的功能
- Android基于名称、修改时间、大小实现文件夹排序