Linux C语言磁盘U盘容量读取、目录列表读取、文件夹大小读取

Linux C语言磁盘U盘容量读取、目录列表读取、文件夹大小读取C语言源代码

#include <stdio.h>
#include <sys/statfs.h>
#include <string.h>
#include <dirent.h>
#include <stdbool.h> 
#include <sys/stat.h>

/***********************************************************
*原型:bool is_dir(const char *path)
*功能:判断是否为目录
***********************************************************/
bool is_dir(const char *path)
{
    struct stat statbuf;
    if(lstat(path, &statbuf) ==0)//lstat返回文件的信息,文件信息存放在stat结构中
    {
        return S_ISDIR(statbuf.st_mode) != 0;//S_ISDIR宏,判断文件类型是否为目录
    }
    return false;
}

/***********************************************************
*原型:bool is_special_dir(const char *path)
*功能:判断是否是特殊目录
***********************************************************/
bool is_special_dir(const char *path)
{
    return strcmp(path, ".") == 0 || strcmp(path, "..") == 0;
}

/***********************************************************
*原型:bool is_file(const char *path)
*功能:判断是否为常规文件
***********************************************************/
bool is_file(const char *path)
{
    struct stat statbuf;
    if(lstat(path, &statbuf) ==0)
        return S_ISREG(statbuf.st_mode) != 0;//判断文件是否为常规文件
    return false;
}

/***********************************************************
*原型:void get_file_path(const char *path, const char *file_name,  char *file_path)
*功能:生成完整的文件路径
***********************************************************/
void get_file_path(const char *path, const char *file_name,  char *file_path)
{
    strcpy(file_path, path);
    if(file_path[strlen(path) - 1] != '/')
        strcat(file_path, "/");
    strcat(file_path, file_name);
	
}

/***********************************************************
*原型:int file_size(char* filename) 
*功能:获取文件的大小,返回文件的字节大小
***********************************************************/
int file_size(char* filename) 
{ 
	int size = 0;
	struct stat sss;
	stat(filename,&sss); 
	size = sss.st_size; 
	return size; 
} 

/***********************************************************
*原型:void FileFilderSize(const char *path,int *cap)
*功能:获取文件夹的大小,返回文件夹的字节大小
***********************************************************/
void FileFilderSize(const char *path,int *cap)
{
	int sum = 0;
	char aaa[200] = {0};
	DIR * dir;
	struct dirent * dir_info;
    char file_path[PATH_MAX];
    if(is_file(path))
    {
        remove(path);
        return;
    }
    if(is_dir(path))
    {
        if((dir = opendir(path)) == NULL)
            return;
        while((dir_info = readdir(dir)) != NULL)
        {
            get_file_path(path, dir_info->d_name, file_path);
            if(is_special_dir(dir_info->d_name))
			{
				continue;
			}
                
			sprintf(aaa,"%s/%s",path,dir_info->d_name);
        	sum += file_size(aaa);
			printf("d_name : %s\n", dir_info->d_name);
			
        }
		closedir(dir);
    }
	*cap = sum;
}
/***********************************************************
*原型:int Udisk_Capacity(char *chardir,int *total,int *free)
*功能:获取U盘(磁盘)空间大小,单位为KB
***********************************************************/
int Udisk_Capacity(char *chardir,int *total,int *free)
{
	struct statfs s;
	
	memset(&s, 0, sizeof(struct statfs));
	if( 0 != statfs(chardir, &s) )
	{
		return -1;
	}

	if(s.f_bsize >= 1024)
	{
		printf("\n if(s.f_bsize >= 1024)\n");
		*total = (int)(  (s.f_bsize / 1024 ) * s.f_blocks );
		*free = (int) ( ( s.f_bsize / 1024 ) * s.f_bavail );
	}
	else
	{
		printf("\n if(s.f_bsize < 1024)  else\n");
		*total = (int)(  (s.f_blocks / 1024 ) * s.f_bsize );
		*free = (int) ( ( s.f_bavail / 1024 ) * s.f_bsize );
	}
}


int main(void)
{
	int a,b,c;
	Udisk_Capacity("/home/xxx/Desktop",&a,&b);
	printf("Disk total space =%d KB %d MB %d GB\n",a,a>>10,a>>20);
	printf("Disk free space=%d KB %d MB %d GB\n",b,b>>10,b>>20);
	FileFilderSize("/home/xxx/Desktop/testfolder/",&c);
	
	printf("FileFolder space = %d bytes %d KB %d MB\n",c,c/1000,c/1000000);
}

测试结果

首先测试一下U盘或者磁盘的大小:

 
在选择一个文件夹查看一下文件夹的总大小:

运行程序后查看结果如下:

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值