在32位系统中,什么是 FTELL 回报,如果以二进制模式打开一个文件的当前位置指示器是过去2GB的意义呢?在 C99 标准,是这种的未定义行为的,因为 FTELL 必须返回一个长整型(最大值为 2 ** 31-1 )?
On a 32-bit system, what does ftell return if the current position indicator of a file opened in binary mode is past the 2GB point? In the C99 standard, is this undefined behavior since ftell must return a long int (maximum value being 2**31-1)?
推荐答案长整型应该是至少有32位,但C99标准没有限制到32位。C99标准确实提供了便利类型,如 int16_t &安培; int32_t 等映射到正确的比特大小的目标平台。
on long int
long int is supposed to be AT LEAST 32-bits, but C99 standard does NOT limit it to 32-bit. C99 standard does provide convenience types like int16_t & int32_t etc that map to correct bit sizes for a target platform.
FTELL()和 fseek的()上绝大多数被限制为32位(包括符号位) 32位架构的系统。因此,当有大型文件的支持,你遇到了这个问题,2GB
ftell() and fseek() are limited to 32 bits (including sign bit) on the vast majority of 32-bit architecture systems. So when there is large file support you run into this 2GB issue.
POSIX.1-2001和SysV为 fseek的和 FTELL 是 fseeko 和 ftello ,因为它们使用off_t作为参数的偏移。
POSIX.1-2001 and SysV functions for fseek and ftell are fseeko and ftello because they use off_t as the parameter for the offset.
你需要定义一个 -D_FILE_OFFSET_BITS = 64 编译或定义它包括stdio.h中,确保之前的地方, off_t 是64位。
you do need to define compile with -D_FILE_OFFSET_BITS=64 or define it somewhere before including stdio.h to ensure that off_t is 64-bits.
阅读这个在 cert安全编码指南。
C99说长整型必须的至少的32位不说,它不能做大
C99 says long int must be at least 32-bits it does NOT say that it cannot be bigger
尝试x86_64体系如下:
try the following on x86_64 architecture:
#include <stdio.h> int main(int argc, char *argv[]) { FILE *fp; fp = fopen( "test.out", "w"); if ( !fp ) return -1; fseek(fp, (1L << 34), SEEK_SET); fprintf(fp, "\nhello world\n"); fclose(fp); return 0; }注意 1L 只是一个长,这将产生一个文件,该文件是17GB和坚持一个\\ nhello世界\\ n来它的结束。您可以验证是否有通过利用平凡尾-n1 test.out 或明确使用:
Notice that 1L is just a long, this will produce a file that's 17GB and sticks a "\nhello world\n" to the end of it. Which you can verify is there by trivially using tail -n1 test.out or explicitly using:
DD如果= test.out跳过= $((1 <<;&LT; 25))
dd if=test.out skip=$((1 << 25))
注意,DD通常使用的块大小(1 LT;&LT; 9)所以 34 - 9 = 25 将倾出'\\ nhello世界\\ n
Note that dd typically uses block size of (1 << 9) so 34 - 9 = 25 will dump out '\nhello world\n'
更多推荐
FTELL在过去2GB的位置
发布评论