正如“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 转换为具有固定大小(C99 标准)的int64_t。
注意:(我的书'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)都支持这个特性。