FTELL在过去2GB的位置

编程入门 行业动态 更新时间:2024-10-25 09:27:39
本文介绍了FTELL在过去2GB的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在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的位置

本文发布于:2023-11-27 10:18:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637667.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   在过去   FTELL   GB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!