When I open a file with the mode FILE_FLAG_NO_BUFFERING
and work with its requirements, notably here that IO must be done in multiples of the volume sector size, the file I am left with has a size that is larger than it needs to be, i.e. 4096 bytes long when the meaningful data in the file is only 3600 bytes long.
The method I'm presently using for this is:
- Initialize two
SIZE_T
variables, onecBuffers
for keeping track of the number of writes where the entire buffer size was occupied with useful data, and another,cbRemainder
for keeping track of the number of bytes used in the last buffer. So, for example, if my sector size and buffer size was 512 and I wrote 3600 bytes of useful data, then, at the end of the IO,cBuffers
would be 7 andcbRemainder
would be 496. - Following IO, calculate the total file offset to useful data, which will be
(cBuffers * BYTES_PER_SECTOR) + cbRemainder
. - Close the unbuffered file handle.
- Reopen the same filename, but this time without the flag
FILE_FLAG_NO_BUFFERING
. - Set the file pointer to the offset to the end of the useful data, and call
SetEndOfFile
, then flush and close the handle.
My question is basically: am I required to do this if I want to set the file size to the end of the valid data if I am doing unbuffered writes? Is it possible to set the file size with the unbuffered handle? Are there other methods or idioms for this task?