fseek到32位无符号偏移量 [英] fseek to a 32-bit unsigned offset

查看:213
本文介绍了fseek到32位无符号偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取一个文件格式(TIFF),它从文件开头有32位无符号偏移量。

I am reading a file format (TIFF) that has 32-bit unsigned offsets from the beginning of the file.

不幸的是fseek的原型,通常的方式我会去特定的文件偏移量,是:

Unfortunately the prototype for fseek, the usual way I would go to particular file offset, is:

int fseek ( FILE * stream, long int offset, int origin );

因此抵消了签名。我应该如何处理这种情况?我应该使用不同的函数进行搜索吗?

so the offset is signed. How should I handle this situation? Should I be using a different function for seeking?

推荐答案

更深入地研究这个问题并考虑其他评论和答案(谢谢) you),我认为最简单的方法是在偏移量大于2147483647字节时进行两次搜索。这允许我将偏移保持为 uint32_t 并继续使用 fseek 。因此定位代码如下:

After studying this question more deeply and considering the other comments and answers (thank you), I think the simplest approach is to do two seeks if the offset is greater than 2147483647 bytes. This allows me to keep the offsets as uint32_t and continue using fseek. The positioning code is therefore like this:

// note: error handling code omitted
uint32_t offset = ... (whatever it is)
if( offset > 2147483647 ){
   fseek( file, 2147483647, SEEK_SET );
   fseek( file, (long int)( offset - 2147483647 ), SEEK_CUR );
} else {
   fseek( file, (long int) offset, SEEK_SET );
}

使用64位类型的问题是代码可能正在运行32位架构(除其他外)。有一个函数 fsetpos 它使用一个结构 fpos_t 来管理任意大的偏移量,但这会带来一系列的复杂性。虽然 fsetpos 可能有意义,如果我真的使用任意大尺寸的偏移,因为我知道最大可能的偏移是uint32_t,那么双重搜索满足了这个需要。

The problem with using 64-bit types is that the code might be running on a 32-bit architecture (among other things). There is a function fsetpos which uses a structure fpos_t to manage arbitrarily large offsets, but that brings with it a range of complexities. Although fsetpos might make sense if I was truly using offsets of arbitrarily large size, since I know the largest possible offset is uint32_t, then the double seek meets that need.

请注意,此解决方案允许在32位系统上处理所有TIFF文件。如果您考虑像PixInsight这样的商业程序,这一点很明显。在32位系统上运行时,PixInsight只能处理小于2147483648字节的TIFF文件。要处理完整大小的TIFF文件,用户必须在64位计算机上使用64位版本的PixInsight。这可能是因为PixInsight程序员使用64位类型在内部处理偏移。由于我的解决方案只使用32位类型,我可以在32位系统上处理完整大小的TIFF文件(只要底层操作系统可以处理大的文件)。

Note that this solution allows all TIFF files to be handled on a 32-bit system. The advantage of this is obvious if you consider commercial programs like PixInsight. PixInsight can only handle TIFF files smaller than 2147483648 bytes when running on 32-bit systems. To handle full sized TIFF files, a user has to use the 64-bit version of PixInsight on a 64-bit computer. This is probably because the PixInsight programmers used a 64-bit type to handle the offsets internally. Since my solution only uses 32-bit types, I can handle full-sized TIFF files on a 32-bit system (as long as the underlying operating system can handle files that large).

这篇关于fseek到32位无符号偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆