正如《 GNU C库参考手册》所说
off_t
This is a signed integer type used to represent file sizes.
In the GNU C Library, this type is no narrower than int.
If the source is compiled with _FILE_OFFSET_BITS == 64 this
type is transparently replaced by off64_t.
和
off64_t
This type is used similar to off_t. The difference is that
even on 32 bit machines, where the off_t type would have 32 bits,
off64_t has 64 bits and so is able to address files up to 2^63 bytes
in length. When compiling with _FILE_OFFSET_BITS == 64 this type
is available under the name off_t.
因此,如果您想要一种可靠的方式来表示客户端和服务器之间的文件大小,则可以:
- 相应地使用
off64_t
类型和stat64()
功能(因为它填充了结构stat64
,其中包含off64_t
类型本身)。类型off64_t
保证在32位和64位计算机上的大小相同。
- 正如前面所提到的编译代码与
-D_FILE_OFFSET_BITS == 64
和使用通常的off_t
和stat()
。
- 兑换
off_t
为int64_t
固定大小的类型(C99标准)。
注意:(我的书《 C in a Nutshell》说这是C99标准,但在实现上是可选的)。最新的C11标准说:
7.20.1.1 Exact-width integer types
1 The typedef name intN_t designates a signed integer type with width N ,
no padding bits, and a two’s complement representation. Thus, int8_t
denotes such a signed integer type with a width of exactly 8 bits.
without mentioning.
关于实现:
7.20 Integer types <stdint.h>
... An implementation shall provide those types described as ‘‘required’’,
but need not provide any of the others (described as ‘‘optional’’).
...
The following types are required:
int_least8_t uint_least8_t
int_least16_t uint_least16_t
int_least32_t uint_least32_t
int_least64_t uint_least64_t
All other types of this form are optional.
因此,通常,C标准不能保证具有固定大小的类型。但是大多数编译器(包括gcc)都支持此功能。
__
内容保留供实现使用(除非标准定义了它的含义,如__func__
或__FILE__
)。间接级别使实现可以定义自己的类型,__off_t
而不会干扰您可以合法做的任何事情。然后可以更好地隐藏标头的特定于平台的位(例如,源代码的单个副本可以在单个计算机上处理32位和64位编译)。读取标准标头是一项繁重的工作,因为存在许多相互关联的定义。