正如《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()
函数(因为它填充包含 off64_t
类型本身的结构 stat64
)。类型 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)支持此功能。
因此,通常情况下,C标准无法确保具有固定大小的类型。但是,大多数编译器(包括gcc)都支持这个特性。