处理大文件(> 2GB)时将 pos_type 分配给 uint64_t 是否安全?

发布于 2024-12-25 18:18:37 字数 254 浏览 4 评论 0原文

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

  • 目标平台:运行当前 Linux 发行版、Windows、Mac 的台式机。
  • 任务:随机二进制文件访问(tellpseekp)。
  • 寻求:最接近标准(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 and seekp).
  • Sought: solution that is closest to the standards (POSIX, Win API), portable and safe.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2025-01-01 18:18:37

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

另一方面,在实践中,您不应该遇到任何问题
基于 Unix 的机器;在Windows下,数值不一定
如果文件以文本模式打开,则意义重大,但您可以将
pos_typeuint64_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 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.

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).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文