在 Linux C++ 应用程序中查找和读取大文件 [英] Seeking and reading large files in a Linux C++ application

查看:28
本文介绍了在 Linux C++ 应用程序中查找和读取大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 G++ 中的标准 ftellfseek 选项遇到整数溢出问题,但我想我错了,因为 ftell64em> 和 fseek64 不可用.我一直在搜索,许多网站似乎都使用 lseekoff64_t 数据类型来引用,但我没有找到任何引用等于 fseek 的示例.现在我正在阅读的文件是 16GB+ CSV 文件,预计至少是两倍.

I am running into integer overflow using the standard ftell and fseek options inside of G++, but I guess I was mistaken because it seems that ftell64 and fseek64 are not available. I have been searching and many websites seem to reference using lseek with the off64_t datatype, but I have not found any examples referencing something equal to fseek. Right now the files that I am reading in are 16GB+ CSV files with the expectation of at least double that.

在没有任何外部库的情况下,实现与 fseek/ftell 对类似的结构的最直接方法是什么?我的应用程序现在使用 4.x 的标准 GCC/G++ 库工作.

Without any external libraries what is the most straightforward method for achieving a similar structure as with the fseek/ftell pair? My application right now works using the standard GCC/G++ libraries for 4.x.

推荐答案

fseek64 是一个 C 函数.要使其可用,您必须在包含系统头文件之前定义 _FILE_OFFSET_BITS=64 这或多或少地将 fseek 定义为实际上是 fseek64.或者在编译器参数中执行,例如gcc -D_FILE_OFFSET_BITS=64 ....

fseek64 is a C function. To make it available you'll have to define _FILE_OFFSET_BITS=64 before including the system headers That will more or less define fseek to be actually fseek64. Or do it in the compiler arguments e.g. gcc -D_FILE_OFFSET_BITS=64 ....

http://www.suse.de/~aj/linux_lfs.html 对 linux 上的大文件支持有一个很好的概述:

http://www.suse.de/~aj/linux_lfs.html has a great overviw of large file support on linux:

  • 使用gcc -D_FILE_OFFSET_BITS=64"编译您的程序.这会强制所有文件访问调用使用 64 位变体.几种类型也会发生变化,例如off_t 变为 off64_t.因此,重要的是始终使用正确的类型并且不要使用例如int 而不是 off_t.为了与其他平台的可移植性,您应该使用 getconf LFS_CFLAGS 它将在 Linux 平台上返回 -D_FILE_OFFSET_BITS=64 但可能会返回其他内容,例如索拉里斯.对于链接,您应该使用通过 getconf LFS_LDFLAGS 报告的链接标志.在 Linux 系统上,您不需要特殊的链接标志.
  • 定义_LARGEFILE_SOURCE 和_LARGEFILE64_SOURCE.通过这些定义,您可以直接使用像 open64 这样的 LFS 函数.
  • 使用 O_LARGEFILE 标志和 open 对大文件进行操作.

这篇关于在 Linux C++ 应用程序中查找和读取大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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