My code looks close to this:
- Lots of writes using normal C++ IO stream insertions like
steram << "foo"; stream.flush();at some point;- MSVC C API’s
_stat()call right after the flush above.
What I am observing is that the _stat() call is returning sizes different from stream.tellp(), smaller sizes.
If I close the stream before calling the _stat() function it returns correct results. I have stepped into the _stat() and it uses FindFirstFileEx() to get the size.
Is this a known Win32 API quirk?
I assume you are working on Windows Vista or higher. The file size was updated in the file attributes under XP every time the file was flushed to disc. This is no longer true on Vista where as Hans already did comment the file attributes and its size is updated when the last handle to the file is closed.
See Old New Thing Blog for more details.
You could try to open the file a second time and close it as well.