Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
844 views
in Technique[技术] by (71.8m points)

c - Flushing fopen()'ed files opened in update mode,between read and write operations.Explicit flushing needed?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You nailed the 2nd point correctly i.e. if we reach EOF and then we intend to write its fine, its just like fseek to SEEK_END.

But for the 1st point it is needed that we take a reference point to start the read operation after write operation i.e. because suppose that you are trying to overwrite some dynamic data in the file so the length may vary depending upon that you may not get the exact position which you are thinking.

example: say file test.txt has data hello world you want to update the hello text to some other text say share so according to you the text should be share world. Hence the length is same you will get the exact output but what about replacing hello with bye or someother text then you will either get garbage data or no data(if replace string is too long)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...