linux系统中用C语言获取sd卡和u盘的容量

zj870951 2010-05-25 10:51:26
小弟我是新手,最近要在程序中获取sd卡和U盘的总容量和剩余容量,请各位大侠帮帮忙,谢谢。
...全文
1407 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lylianlll 2010-05-26
  • 打赏
  • 举报
回复
如果是负数的话, 前面加上一句 typedef unsigned __int64 U64;
zj870951 2010-05-26
  • 打赏
  • 举报
回复
负数问题已经解决了 谢谢帮忙
yanjinbin0 2010-05-25
  • 打赏
  • 举报
回复
我也想知道,关注.
yanjinbin0 2010-05-25
  • 打赏
  • 举报
回复
获取挂载的文件大小就是容量大小啊,又学了一招....
一品劏 2010-05-25
  • 打赏
  • 举报
回复
.............
zj870951 2010-05-25
  • 打赏
  • 举报
回复
谢谢 我去试试
lylianlll 2010-05-25
  • 打赏
  • 举报
回复

int GetStorageInfo(char * MountPoint, //SD卡随便一个分区
int *Capacity, // 想要获取的空间大小
int type) //获取什么类型的空间
{
struct statfs statFS; //系统stat的结构体
U64 usedBytes = 0;
U64 freeBytes = 0;
U64 totalBytes = 0;
U64 endSpace = 0;

if (statfs(MountPoint, &statFS) == -1){ //获取分区的状态
printf("statfs failed for path->[%s]\n", MountPoint);
return(-1);
}

totalBytes = (U64)statFS.f_blocks * (U64)statFS.f_frsize; //详细的分区总容量, 以字节为单位
freeBytes = (U64)statFS.f_bfree * (U64)statFS.f_frsize; //详细的剩余空间容量,以字节为单位
usedBytes = (U64)(totalBytes - freeBytes); //详细的使用空间容量,以字节为单位

switch( type )
{
case 1:
endSpace = totalBytes/1024; //以KB为单位的总容量
break;

case 2:
endSpace = usedBytes/1024; //以KB为单位的使用空间
break;

case 3:
endSpace = freeBytes/1024; //以KB为单位的剩余空间
break;

default:
return ( -1 );
}
*Capacity = endSpace; //这个不用说了吧
return 0;
}
zj870951 2010-05-25
  • 打赏
  • 举报
回复
我是想说这几个参数分别表示什么呢?
zj870951 2010-05-25
  • 打赏
  • 举报
回复
2楼 谢谢你的回帖
不过看的不是很懂,能否解释下。
lylianlll 2010-05-25
  • 打赏
  • 举报
回复
int GetStorageInfo(char * MountPoint, int *Capacity, int type)
{
struct statfs statFS;
U64 usedBytes = 0;
U64 freeBytes = 0;
U64 totalBytes = 0;
U64 endSpace = 0;

if (statfs(MountPoint, &statFS) == -1){
printf("statfs failed for path->[%s]\n", MountPoint);
return(-1);
}

totalBytes = (U64)statFS.f_blocks * (U64)statFS.f_frsize;
freeBytes = (U64)statFS.f_bfree * (U64)statFS.f_frsize;
usedBytes = (U64)(totalBytes - freeBytes);

switch( type )
{
case 1:
endSpace = totalBytes/1024;
break;

case 2:
endSpace = usedBytes/1024;
break;

case 3:
endSpace = freeBytes/1024;
break;

default:
return ( -1 );
}
*Capacity = endSpace;
return 0;
}

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧