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

标签 python c file-io

据推测,这一行应该没有效果;从当前位置开始寻找 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/

相关文章:

python - Keras 加载彩色图像

python - 阅读 setup.py 中的 README

python - 以编程方式将图片导入 Apple Photos 并修改 Exif 数据

c - Perl(或C printf)后退一个选项卡,可能吗?

java - FileNotFoundException - 进程无法访问文件

c++ - 大约 2Gb 后 C++ ofstream 写入性能下降

java - 使用 getResourceAsStream() 到项目文件夹外部的文件的绝对路径

WINDOWS 上的 Python flask 双栈(ipv4 和 ipv6)

javascript - emscripten 在 C 中读取 javascript arrayBuffer

iphone - 比较 Objective-C 中的 float 和 double 数据类型