当前位置: 技术问答>linux和unix
linux系统中用C语言获取sd卡和u盘的容量
来源: 互联网 发布时间:2016-08-29
本文导语: 小弟我是新手,最近要在程序中获取sd卡和U盘的总容量和剩余容量,请各位大侠帮帮忙,谢谢。 | int GetStorageInfo(char * MountPoint, int *Capacity, int type) { struct statfs statFS; U64 ...
小弟我是新手,最近要在程序中获取sd卡和U盘的总容量和剩余容量,请各位大侠帮帮忙,谢谢。
|
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;
}
{
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;
}
|
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;
}
|
我也想知道,关注.