问题描述
我已阅读本关于读之间的切换操作和写操作(反之亦然)的文件使用打开更新 fopen()函数
()
I have read this about the switch between read and write operations(and vice-versa) for files opened for update using fopen()
(LINK)
的打开文件进行更新(这其中包括一个+号),在其输入和输出操作是允许的,流应被刷新(fflush)或重新定位(fseek的,fsetpos,倒带)之间任一个写入操作之后是读操作还是读出操作而没有达到结束文件随后的写入动作。的
有这里提到的两件事情,我想强调
There are two things mentioned here that I would like to highlight
- 流应该被刷新(fflush)或重新定位(fseek的,fsetpos,倒带)无论是写操作之后读取操作 之间
- 或读出操作,它没有达到结束文件随后的写入操作。强>
- 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)关于第一点,我们做的明确地需要写入之间冲洗和读operation.I意味着,假设如果我们打算写一个新的文件,然后阅读back.In使用 fseek的(),这种情况下
或退()
去年初写操作之后的文件是有道理的,作为一个副作用,它会刷新我想知道的是buffer.But,会发生什么,如果我们正在更新/用新数据覆盖现有文件的第一部分,但一旦完成,我们要为立即开始从该点读余下的,旧的数据?我们是否需要使用像 fseek的(PFILE,0,SEEK_CUR)
让我们刷新缓冲区的写 - 读
过渡,并在同一时间,不改变文件指针的位置?同样的困惑,从第二个场景出现为好,当我们要读取一个文件的前半部分,和后立即要开始写在那里。请回答这个问题。
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)关于读取操作的第二部分而没有达到最终的文件
似乎暗示,如果读操作到达我们打算以后写结束的文件,我们不需要缓冲被刷新该读写
transition.Can你确认那是什么这意味着什么?只有写 - 读
过渡需要冲洗,而不是读写
过渡,特别是阅读已达到端值的文件?
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?
推荐答案
您钉,如果我们EOF到达,然后我们打算写的很好,第二点正确的,即它就像 fseek的
为 SEEK_END
。
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.
例如:说文件test.txt中有数据的Hello World
要更新的你好
文本到其他文字说共享
所以要根据你的文字应该是占世界
。因此,长度是一样的,你会得到确切的输出,但怎么样替换你好
与再见
或 someother
文本,然后你会要么得到垃圾数据或没有数据(如果替换字符串太长)
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)
这篇关于在更新模式打开法拉盛fopen()函数“执行的文件,读之间和冲洗需要写operations.Explicit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!