Fflush - how to check if the operation was the last operation

From the documentation for std :: fflush ( http://en.cppreference.com/w/cpp/io/c/fflush ):

Makes the stream of the output file synchronize with the actual contents of the file. The behavior is undefined if the given stream is equal to the input type or if the given stream has the update type, but the last I / O operation was not an output operation.

I need to call fflush on a file in order to be able to get its size on disk, but I don't know if the last operation was entered or output. Is there a way to check if the last operation in FILE was output in order to prevent undefined behavior?

+6
source share
1 answer

From the documentation :

In files open for updating (i.e. open for reading and writing), the stream must be flushed after the output operation before performing the input operation. This can be done either by permutation (fseek, fsetpos, rewind), or by explicitly calling fflush

Then IMO you should just avoid calling fflush (even if it should be safe for a file open for I / O) and use fseek to move the cursor.

+5
source

All Articles