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; } //以下省略 我看下來,基本的流程應(yīng)該是: 1)先將不足一個扇區(qū)的內(nèi)容寫完,然后OffsetofSector = 0;如果沒寫完,以后會執(zhí)行最外層if/else分支中的else分支。 2)現(xiàn)在的問題就出在最外層的else分支 其基本的流程應(yīng)該是先寫滿當前的簇,以后會從簇的起始扇區(qū)寫 if(writeLength > DeviceInfo.BPB_BytesPerSec) { step = writeLength / DeviceInfo.BPB_BytesPerSec; //step表示需要寫多少個整數(shù)扇區(qū) //此處的條件判斷,我想來想去似乎應(yīng)該這樣表示 if( (step + ThisFile.SectorofCluster) > DeviceInfo.BPB_SecPerClus) { step = DeviceInfo.BPB_SecPerClus - ThisFile.SectorofCluster; len = step * DeviceInfo.BPB_BytesPerSec; } else len = step * DeviceInfo.BPB_BytesPerSec; //因為如果第一次在一個簇內(nèi)寫入時,ThisFile.SectorofCluster并不一定為零 //怎么能用step作判據(jù)呢?應(yīng)該是先考慮當前的簇的剩余空間是否夠用。 //如果還沒寫完,會從一個新簇開始寫,此時因為ThisFile.SectorofCluster = 0, //這兩種才沒有區(qū)別 } else { step = 1; len = writeLength; }
我的問題見注釋處,百思不得其解,還望高手指點迷津,謝謝。