Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fflush - how to check if last operation was output operation

Tags:

c++

c

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?

like image 206
Mircea Ispas Avatar asked Dec 16 '13 14:12

Mircea Ispas


1 Answers

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.

like image 112
Adriano Repetti Avatar answered Oct 16 '22 21:10

Adriano Repetti