c - 为什么 ftell 的返回类型不是 fpos_t?

标签 c c99

根据 C99,ftell 的原型(prototype)是:

long int ftell(FILE *stream);

根据我的理解,它应该是以下内容:

fpos_t ftell(FILE *stream);

这是为什么?

来自§7.19.1-2

fpos_t which is an object type other than an array type capable of recording all the information needed to specify uniquely every position within a file.

我知道 fpos_t 应该用来记录文件中的位置。所以 ftell 返回文件中的位置应该是那种类型。相反,它是:

  • 签名
  • long 类型,它可能太小或太大而无法在某些体系结构上访问文件。

最佳答案

注意 fpos_t

[...] a complete object type other than an array type capable of recording all the information needed to specify uniquely every position within a file.

所以它甚至可以是一个结构,除了调用 fsetpos 之外完全不能用于任何其他用途!

另一方面,ftell 的返回值是一个标量,保证可用于告知二进制文件中的确切字节位置:

For a binary stream, the value is the number of characters from the beginning of the file.


除此之外,原因是向后兼容性ftell 在 C89 中首次亮相,也许当时的期望是 long 可以足够快地扩展以包含所有文件大小,但如今情况并非总是如此。不幸的是,不可能更改 ftell 返回的类型,但现在更改它为时已晚 - 即使那些支持更大文件的平台现在也有另一个名称的函数,例如 ftello.


需要签名,因为该函数在错误时返回 -1

关于c - 为什么 ftell 的返回类型不是 fpos_t?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57200729/

相关文章:

c++ - GTK 窗口配置事件未传播

c - 结构处理和结构数组

c - 全局变量是否在函数调用之间刷新?

c - 在代码中出现运行时错误

c - 浮点方程检查 ansi c - isnormal()

c - 使用C99标准导致GCC编译错误

c - 如何在线程安全 C 库中高效实现句柄

c - 监视文件更改的可移植方式

c++ - 如何将函数指针传递给 C++ 中的函数?

c - 尝试从文件中读取数字,但不断收到段错误 :11. -C