Set correct file size on handle opened with `FILE_FLAG_NO_BUFFERING` without clsoing and reopening the file?

77 views Asked by At

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:

  1. Initialize two SIZE_T variables, one cBuffers 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 and cbRemainder would be 496.
  2. Following IO, calculate the total file offset to useful data, which will be (cBuffers * BYTES_PER_SECTOR) + cbRemainder.
  3. Close the unbuffered file handle.
  4. Reopen the same filename, but this time without the flag FILE_FLAG_NO_BUFFERING.
  5. 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?

0

There are 0 answers