处理大文件(> 2GB)时将 pos_type 分配给 uint64_t 是否安全?
当尝试以跨平台方式处理大文件(2/4GB)时,将 pos_type
转换为 uint64_t
是否安全?
- 目标平台:运行当前 Linux 发行版、Windows、Mac 的台式机。
- 任务:随机二进制文件访问(
tellp
和seekp
)。 - 寻求:最接近标准(POSIX、Win API)、可移植且安全的解决方案。
When trying to deal with large files (2/4GB) in cross-platform manner, is it safe to cast pos_type
to uint64_t
?
- Platforms targeted: desktop machines running current Linux distro, Windows, Mac.
- Task: random binary file access (
tellp
andseekp
). - Sought: solution that is closest to the standards (POSIX, Win API), portable and safe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在实践中或理论上。就标准而言,我不
认为可以保证
pos_type
甚至可以转换为整型;从逻辑上讲,它不应该是,因为它包含几个
独立数据:距文件开头的偏移量和
多字节解码的状态信息。
另一方面,在实践中,您不应该遇到任何问题
基于 Unix 的机器;在Windows下,数值不一定
如果文件以文本模式打开,则意义重大,但您可以将
pos_type
到uint64_t
并返回,不会丢失值(除非有确实是原始 pos_type 中重要的多字节状态,但我
不知道 Windows 下有任何编码会出现这种情况)。
应该可以在编译时确定
pos_type
是否隐式转换为整型,并在某些情况下使用它
有点像
static_assert
。然而,我认为这并没有什么意义。它不会确定积分值是否以任何方式可用,除了
重新转换回
pos_type
。 (这可能是某种魔法例如,cookie。但我不会太担心。这
标准允许很多没有合理实施的事情
做。请记住,即使在 Windows 下,该值并不总是
表示可以读取的确切字节数。
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 toan 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.
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 auint64_t
and back without loss of value (unless therereally is significant multibyte state in the original
pos_type
, but Idon't know of any encoding under Windows where this would be the case).
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; itwon'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 magiccookie, 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.