I have read this about the switch between read and write operations(and vice-versa) for files opened for update using fopen()
(LINK)
"For files open for update (those which include a "+" sign), on which both input and output operations are allowed, the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a writing operation followed by a reading operation or a reading operation which did not reach the end-of-file followed by a writing operation."
There are two things mentioned here that I would like to highlight
- the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a writing operation followed by a reading operation
- or a reading operation which did not reach the end-of-file followed by a writing operation.
1) Regarding the first point,do we explicitly need to flush between a write and read operation.I mean,suppose if we intend to write to a new file,and then read it back.In this case using fseek()
or rewind()
to get to the beginning of the file after the write makes sense,and as a side-effect it will flush the buffer.But what I want to know is, what would happen,if we are updating/overwriting the first part of an already existing file with new data,but once done,we want to immediately begin reading the remaining,old data from that point?Do we need to use something like fseek(pFile,0,SEEK_CUR)
so that we flush the buffer for the write-read
transition, and at the same time,doesn't change the position of the file pointer? The same confusion arises from the second scenario as well,when we want to read the first half of a file,and immediately after that want to start writing there. Please answer this.
2) The second part about reading operation which did not reach the end-of-file
seems to imply that if a read operation reaches the end-of-file and we intend to write after that,we don't need the buffer to be flushed for this read-write
transition.Can you confirm it that's what it means?Only write-read
transition needs flushing and not read-write
transition, especially if read has reached end-of-file?