FAT计算可用磁盘空间 [英] FAT-Calculating free disk space
问题描述
大家好,
我要计算SD卡的可用空间. SD卡已格式化为FAT16或
到FAT32.
计算是通过M16C uC执行的,该M16C uC通过SPI连接到卡.
我有一些计算方法的线索,但是我不知道我的想法是否有很高的表现.
我的主意:
从参数引导扇区"中读取以下内容
-总扇区(0x13/0x20)
-每个群集的扇区(0x0d)
-每个扇区的字节数(0x0b)
然后计算
总扇区/每个群集的扇区=>群集总数
然后我查找FAT并计算出总簇中有多少簇是免费的
例如
Hello everybody,
I want to calculate the free space of a SD-Card. The SD-Card is either formatted to FAT16 or
to FAT32.
The calculation is performed by a M16C uC which is connected via SPI to the card.
I have a little clue how to calculate but I don''t know if my idea is high in performance.
My idea:
Reading following from parameters "Boot sector"
- Total sector (0x13/0x20)
- Sector per cluster (0x0d)
- Bytes per sector (0x0b)
Then calculating
Total sector / Sectors per cluster=> Total clusters
Then I look up the FAT and calculate how much clusters of total cluster are free
For example
free_cluster=0;
for(int i=0;i<total mode="hold" />{
if(FAT[i]==0)//Then cluster is free
free_cluster++;
}
最后,我将像这样计算大小:
Finally, I would calculate the size like this:
Size_of_cluster=Bytes per sector*Sector per cluster;
free_space=free_cluster * Size_of_cluster;
您如何看待我的解决方案?
还有其他方法可以解决问题吗?
如果插入SD卡,Windows如何计算?
您将如何解决?
感谢您的帮助.
What do you think about my solution?
Is there any other way how to solve the problem?
How does Windows calculate this if I insert a SD-Card?
How would you solve it?
Thank you for helping
推荐答案
您可以只使用GetDiskFreeSpaceEx
(您需要知道Windows映射SD卡到的驱动器名称).
示例:
-----------------
You can just useGetDiskFreeSpaceEx
(you need to know the drive name that Windows has mapped the SD card to).
Example:
-----------------
int _tmain()
{
ULARGE_INTEGER freeBytesAvailable;
ULARGE_INTEGER totalNumberOfBytes;
ULARGE_INTEGER totalNumberOfFreeBytes;
GetDiskFreeSpaceEx(L"J:\\", &freeBytesAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes);
}
您的计算可用空间的方法似乎很好.
虽然在实现中确实发现了一个错误:
FAT的前两个条目不映射到群集,但包含其他信息.这是2x16或2x32位,具体取决于您使用的是FAT16还是FAT32.尽管此数据无论如何都不会为0,但不应在计算中使用.
Your method to calculate the free space seems fine.
I do spot one error though in the implementation:
The first two entries of the FAT don''t map to clusters but hold other information. This is either 2x16 or 2x32 bits depending on if you''re using FAT16 or FAT32. Although this data won''t be 0 anyway, it shouldn''t be used in the calculation.
这篇关于FAT计算可用磁盘空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!