C++ 中的大文件支持 [英] Large file support in C++

查看:32
本文介绍了C++ 中的大文件支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

64 位文件 API 因平台而异.

64bit file API is different on each platform.

在 Windows 中:_fseeki64
在 linux 中:fseeko
在 freebsd 中:另一个类似的调用......

in windows: _fseeki64
in linux: fseeko
in freebsd: yet another similar call ...

我怎样才能最有效地使它更方便和便携?有什么有用的例子吗?

How can I most effectively make it more convenient and portable? Are there any useful examples?

推荐答案

大多数基于 POSIX 的平台都支持_FILE_OFFSET_BITS"预处理器符号.将其设置为 64 将导致 off_t 类型为 64 位而不是 32 位,并且诸如 lseek() 之类的文件操作函数将自动支持通过一些预处理器魔术的 64 位偏移.从编译时的角度来看,以这种方式添加 64 位文件偏移支持是相当透明的,假设您正确使用了相关的 typedef.当然,如果您公开使用 off_t 类型的接口,您的 ABI 会发生变化.理想情况下,您应该在命令行上定义它,例如:

Most POSIX-based platforms support the "_FILE_OFFSET_BITS" preprocessor symbol. Setting it to 64 will cause the off_t type to be 64 bits instead of 32, and file manipulation functions like lseek() will automatically support the 64 bit offset through some preprocessor magic. From a compile-time point of view adding 64 bit file offset support in this manner is fairly transparent, assuming you are correctly using the relevant typedefs. Naturally, your ABI will change if you're exposing interfaces that use the off_t type. Ideally you should define it on the command line, e.g.:

cxx -D_FILE_OFFSET_BITS=64

确保它适用于您的代码中包含的所有操作系统标头.

to make sure it applies to all OS headers included by your code.

不幸的是,Windows 不支持此预处理器符号,因此您要么必须自己处理它,要么依赖提供跨平台大文件支持的库.ACE 就是这样的一个库(基于 POSIX 和 Windows 平台 - 只需定义 <强>_FILE_OFFSET_BITS=64 在这两种情况下).我知道 Boost.filesystem 也支持大POSIX 平台上的文件,但我不确定 Windows.其他跨平台库可能会提供类似的支持.

Unfortunately, Windows doesn't support this preprocessor symbol so you'll either have to handle it yourself, or rely on a library that provides cross-platform large file support. ACE is one such library (both POSIX based and Windows platforms - just define _FILE_OFFSET_BITS=64 in both cases). I know that Boost.filesystem also supports large files on POSIX based platforms, but I'm not sure about Windows. Other cross-platform libraries likely provide similar support.

这篇关于C++ 中的大文件支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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