C++ sector aligned read

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 01:46:34

My question is, if we need to read sector aligned data, if we use SetFilePointer() for example like this:

SetFilePointer(device, 12, NULL, FILE_BEGIN);

... then you are no longer reading sector-aligned data, and you'll get error 87 in the ReadFile call. Reading sector-aligned data doesn't just mean that you have to read in sector-sized blocks, but you must always read blocks that start on sector boundaries.

You have to seek to the sector containing the bytes of your interest (so, position/sector_size*sector_size), read the whole sector and extract the bytes of your interest from the data you read.

Well, it depends..

  • if you want what's in your buffer to represent an entire sector of the device, and map it using a struct* or byte offsets - that's usually how it's done. then your offsets sent to SetFilePointer should be aligned on the sector size, then read sector sized buffers. So SetFilePointer(0) -> ReadFile(512 bytes)

  • If you don't care, and just want bytes 12-16, SetFilePointer(12) -> Read(4bytes).

I'd go for solution 1, because it would probably make the code easier to read and maintain in the long run.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!