gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:37:55 25 4
gpt4 key购买 nike

根据 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/

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