gpt4 book ai didi

python - fseek(fp, 0, SEEK_CUR) 的副作用是什么

转载 作者:行者123 更新时间:2023-12-04 08:28:20 25 4
gpt4 key购买 nike

据推测,这一行应该没有效果;从当前位置开始寻找 0 个字节。但是,我在一些遗留代码中找到了这一行,如果没有这一行,似乎没有任何效果——即使在 python 中,使用等效的 fp.seek(0, os.SEEK_CUR)

它只是一个缓冲区,还是更深层次的东西?

最佳答案

C 标准规定,在对同一文件句柄的读取写入 之间切换时,您必须 查找。 C11 7.21.5.3p7 :

  1. 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 the fflush function or to a file positioning function (fseek, fsetpos, or rewind), 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 :

  1. After determining the new position, a successful call to the fseek function undoes any effects of the ungetc function on the stream, clears the end-of-file indicator for the stream, and then establishes the new position. After a successful fseek 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com