From std::fflush documentation(http://en.cppreference.com/w/cpp/io/c/fflush):
Causes the output file stream to be synchronized with the actual contents of the file. The behavior is undefined if the given stream is of the input type or if the given stream is of the update type, but the last I/O operation was not an output operation.
I need to call fflush on a file to be able to get its size on disk, but I don't know if the last operation was input or output. Is there a way to check if the last operation on a FILE was output operation in order to prevent undefined behavior?
From documentation:
In files open for update (i.e., open for both reading and writting), the stream shall be flushed after an output operation before performing an input operation. This can be done either by repositioning (fseek, fsetpos, rewind) or by calling explicitly fflush
Then IMO you should simply avoid to call fflush
(even if it should be safe for a file open for I/O) and use fseek
to move cursor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With