据推测,这一行应该没有效果;从当前位置开始寻找 0 个字节。 但是,我在一些遗留代码中找到了这一行,如果没有这一行,似乎没有任何效果——即使在 python 中,使用等效的 fp.seek(0, os.SEEK_CUR)
它只是一个缓冲区,还是更深层次的东西?
最佳答案
C 标准规定,在对同一文件句柄的读取 和写入 之间切换时,您必须 查找。 C11 7.21.5.3p7 :
- When a file is opened with update mode (
'+'
as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to thefflush
function or to a file positioning function (fseek
,fsetpos
, orrewind
), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file. [...]
C 标准修订版中 fseek
的所有副作用 C11 7.21.9.2p5 :
- After determining the new position, a successful call to the
fseek
function undoes any effects of theungetc
function on the stream, clears the end-of-file indicator for the stream, and then establishes the new position. After a successfulfseek
call, the next operation on an update stream may be either input or output.
并非所有这些都适用于 Python 3,因为 Python 3 实际上会尽可能绕过 C stdio; Python 甚至不公开 ungetc
。 Python 2 文件是 stdio 流的更薄包装器。
关于python - fseek(fp, 0, SEEK_CUR) 的副作用是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65144517/