iostream 和大文件支持

发布于 2024-07-15 11:12:54 字数 296 浏览 6 评论 0原文

我试图找到一个明确的答案,但找不到,所以我希望有人知道。

我正在 Linux(32 位操作系统)上使用 GCC 4.x 开发 C++ 应用程序。 该应用程序需要能够读取文件> 大小为2GB。

我真的很想使用 iostream 的东西与 FILE 指针,但我找不到大文件 #defines (_LARGEFILE_SOURCE、_LARGEFILE64_SOURCE、_FILE_OFFSET_BITS=64) 是否对 iostream 标头有任何影响。

我正在32位系统上编译。 任何指示都会有帮助。

I'm trying to find a definitive answer and can't, so I'm hoping someone might know.

I'm developing a C++ app using GCC 4.x on Linux (32-bit OS). This app needs to be able to read files > 2GB in size.

I would really like to use iostream stuff vs. FILE pointers, but I can't find if the large file #defines (_LARGEFILE_SOURCE, _LARGEFILE64_SOURCE, _FILE_OFFSET_BITS=64) have any effect on the iostream headers.

I'm compiling on a 32-bit system. Any pointers would be helpful.

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

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

发布评论

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

评论(2

无声无音无过去 2024-07-22 11:12:54

这在编译 libstdc++ 时已经为您决定,通常取决于 _GLIBCXX_USE_LFS 是否在 c++config.h 中定义。

如果有疑问,请通过 readelf -r (或通过 strings)传递您的可执行文件(或 libstdc++.so,如果动态链接到它)查看您的二进制文件/libstdc++是否链接到fopen/fseek/etc。 或fopen64/fseek64/等。

更新

只要您不需要/尝试fseekftell(您只需从流中读取或写入。)

This has already been decided for you when libstdc++ was compiled, and normally depends on whether or not _GLIBCXX_USE_LFS was defined in c++config.h.

If in doubt, pass your executable (or libstdc++.so, if linking against it dynamically) through readelf -r (or through strings) and see if your binary/libstdc++ linked against fopen/fseek/etc. or fopen64/fseek64/etc.

UPDATE

You don't have to worry about the 2GB limit as long as you don't need/attempt to fseek or ftell (you just read from or write to the stream.)

东京女 2024-07-22 11:12:54

如果您使用 GCC,则可以利用名为 __gnu_cxx::stdio_filebuf 的 GCC 扩展,它将 IOStream 与标准 C FILE 描述符联系起来。

您需要定义以下两件事:

_LARGEFILE_SOURCE

_FILE_OFFSET_BITS=64

例如:

#include <cstdio>
#include <fstream>
#include <ext/stdio_filebuf.h>

int main()
{
  std::ofstream outstream;
  FILE* outfile;

  outfile = fopen("bigfile", "w");

  __gnu_cxx::stdio_filebuf<char> fdbuf(outfile, std::ios::out |
                                       std::ios::binary);
  outstream.std::ios::rdbuf(&fdbuf);

  for(double i = 0; i <= 786432000000.0; i++) {
    outstream << "some data";

  fclose(outfile);
  return 0;

}

If you are using GCC, you can take advantage of a GCC extension called __gnu_cxx::stdio_filebuf, which ties an IOStream to a standard C FILE descriptor.

You need to define the following two things:

_LARGEFILE_SOURCE

_FILE_OFFSET_BITS=64

For example:

#include <cstdio>
#include <fstream>
#include <ext/stdio_filebuf.h>

int main()
{
  std::ofstream outstream;
  FILE* outfile;

  outfile = fopen("bigfile", "w");

  __gnu_cxx::stdio_filebuf<char> fdbuf(outfile, std::ios::out |
                                       std::ios::binary);
  outstream.std::ios::rdbuf(&fdbuf);

  for(double i = 0; i <= 786432000000.0; i++) {
    outstream << "some data";

  fclose(outfile);
  return 0;

}

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