在处理大文件(> 2GB)时,是否可以安全地将pos_type分配给uint64_t? [英] Is it safe to assign pos_type to uint64_t when dealing with large files (> 2GB)?

查看:183
本文介绍了在处理大文件(> 2GB)时,是否可以安全地将pos_type分配给uint64_t?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试以跨平台方式处理大文件(2 / 4GB)时,将 pos_type 转换为 uint64_t

When trying to deal with large files (2/4GB) in cross-platform manner, is it safe to cast pos_type to uint64_t?


  • 定位平台:运行当前Linux发行版,Windows,Mac的台式机。

  • 任务:随机二进制文件访问( tellp seekp )。

  • <
  • Platforms targeted: desktop machines running current Linux distro, Windows, Mac.
  • Task: random binary file access (tellp and seekp).
  • Sought: solution that is closest to the standards (POSIX, Win API), portable and safe.

推荐答案

在实践中或在理论上。就标准而言,我不是
认为有一个保证, pos_type 甚至可以转换为
一个整数类型;逻辑上它不应该是,因为它包含多个
独立数据:从文件开始的偏移
多字节解码的状态信息。

In practice or in theory. As far as the standard is concerned, I don't think that there is a guarantee that pos_type is even convertible to an integral type; logically, it shouldn't be, since it contains several independent data: the offset from the beginning of the file and state information for multibyte decoding.

在实践中,另一方面,你不应该对
基于Unix的机器有任何问题;在Windows下,如果文件在文本模式下打开,数值不一定是
,但可以将
pos_type 转换为 uint64_t 并且不会丢失值(除非
真的是原始的 pos_type 中的重要多字节状态,但我
不知道在Windows下的任何编码,这将是这种情况)。

In practice, on the other hand, you shouldn't have any problems with Unix based machines; under Windows, the numeric value won't necessarily mean much if the file is opened in text mode, but you can convert a pos_type to a uint64_t and back without loss of value (unless there really is significant multibyte state in the original pos_type, but I don't know of any encoding under Windows where this would be the case).

应该可以在编译时确定 pos_type
是否会隐式转换为整数类型或者不是,并且在某些
类别 static_assert 中使用它。然而,我不认为这买多了; it
不会确定整数值是否以任何方式可用,除了
以重新转换回 pos_type 。 (例如,它可能是某种奇怪的
cookie,但我不会担心它太多。
标准允许很多事情,没有合理的实现
。请记住,即使在Windows下,该值并不总是
表示可以读取的确切字节数。

It should be possible to determine at compile time whether pos_type will convert implicitly to an integral type or not, and use this in some sort of static_assert. I don't think this buys much, however; it won't determine whether the integral value is in any way usable except to reconvert back to a pos_type. (It might be some sort of magic cookie, for example. But I wouldn't worry about it too much. The standard allows a lot of things that no reasonable implementation will do. Just keep in mind that even under Windows, the value doesn't always represent the exact number of bytes which can be read.

这篇关于在处理大文件(> 2GB)时,是否可以安全地将pos_type分配给uint64_t?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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