CH375FAT中HPI.C中有一段WriteFile的源代码 while(writeLength > 0) { if(ThisFile.OffsetofSector > 0) { if(writeLength + ThisFile.OffsetofSector > DeviceInfo.BPB_BytesPerSec) len = DeviceInfo.BPB_BytesPerSec; else len = writeLength + ThisFile.OffsetofSector;

if(!RBC_Read(ThisFile.SectorPointer,1,DBUF)) { CHRspBlock.errcode = ERC_DEVICEERR; return FALSE; } len = len - ThisFile.OffsetofSector; for(i = 0; i< len;i++) DBUF[ThisFile.OffsetofSector + i] = *(pBuffer + i);

if(!RBC_Write(ThisFile.SectorPointer,1,DBUF)) { CHRspBlock.errcode=ERC_DEVICEERR; return FALSE; } ThisFile.OffsetofSector = ThisFile.OffsetofSector + len; } else { if(writeLength > DeviceInfo.BPB_BytesPerSec) { step = writeLength / DeviceInfo.BPB_BytesPerSec; if(step > DeviceInfo.BPB_SecPerClus) { step = DeviceInfo.BPB_SecPerClus - ThisFile.SectorofCluster; len = step * DeviceInfo.BPB_BytesPerSec; } else { step = step - ThisFile.SectorofCluster; len = step * DeviceInfo.BPB_BytesPerSec; } } else { step = 1; len = writeLength; } if(!RBC_Write(ThisFile.SectorPointer, step, pBuffer + CHRspBlock.len)) { CHRspBlock.errcode = ERC_DEVICEERR; return FALSE; } ThisFile.OffsetofSector = len; } //以下省略 我看下来,基本的流程应该是: 1)先将不足一个扇区的内容写完,然后OffsetofSector = 0;如果没写完,以后会执行最外层if/else分支中的else分支。 2)现在的问题就出在最外层的else分支 其基本的流程应该是先写满当前的簇,以后会从簇的起始扇区写 if(writeLength > DeviceInfo.BPB_BytesPerSec) { step = writeLength / DeviceInfo.BPB_BytesPerSec; //step表示需要写多少个整数扇区 //此处的条件判断,我想来想去似乎应该这样表示 if( (step + ThisFile.SectorofCluster) > DeviceInfo.BPB_SecPerClus) { step = DeviceInfo.BPB_SecPerClus - ThisFile.SectorofCluster; len = step * DeviceInfo.BPB_BytesPerSec; } else len = step * DeviceInfo.BPB_BytesPerSec; //因为如果第一次在一个簇内写入时,ThisFile.SectorofCluster并不一定为零 //怎么能用step作判据呢?应该是先考虑当前的簇的剩余空间是否够用。 //如果还没写完,会从一个新簇开始写,此时因为ThisFile.SectorofCluster = 0, //这两种才没有区别 } else { step = 1; len = writeLength; }

我的问题见注释处,百思不得其解,还望高手指点迷津,谢谢。