对于大文件,fread 会失败吗?
我必须分析一个 16 GB 的文件。我正在使用 fread()
和 fseek()
顺序读取文件。可行吗? fread()
能处理这么大的文件吗?
I have to analyze a 16 GB file. I am reading through the file sequentially using fread()
and fseek()
. Is it feasible? Will fread()
work for such a large file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你没有提到语言,所以我假设是 C。
我没有发现
fread
有任何问题,但是fseek
和ftell可能有问题。
这些函数使用
long int
作为数据类型来保存文件位置,而不是像fpos_t
甚至size_t
这样的智能类型。这意味着它们可能无法处理超过 2 GB 的文件,并且肯定无法处理 16 GB 的文件。您需要查看您的平台上的
long int
有多大。如果是 64 位的就可以了。如果是 32,则在使用ftell
测量距文件开头的距离时可能会遇到问题。考虑使用
fgetpos
和fsetpos
代替。You don't mention a language, so I'm going to assume C.
I don't see any problems with
fread
, butfseek
andftell
may have issues.Those functions use
long int
as the data type to hold the file position, rather than something intelligent likefpos_t
or evensize_t
. This means that they can fail to work on a file over 2 GB, and can certainly fail on a 16 GB file.You need to see how big
long int
is on your platform. If it's 64 bits, you're fine. If it's 32, you are likely to have problems when usingftell
to measure distance from the start of the file.Consider using
fgetpos
andfsetpos
instead.感谢您的回复。我知道我哪里出了问题。
fseek()
和ftell()
不适用于大于 4GB 的文件。我使用了 _fseeki64() 和 _ftelli64() ,现在工作正常。Thanks for the response. I figured out where I was going wrong.
fseek()
andftell()
do not work for files larger than 4GB. I used_fseeki64()
and_ftelli64()
and it is working fine now.如果正确实施,这应该不是问题。我假设按顺序你的意思是你正在以离散的块查看文件并前进你的文件指针。
查看 http://www. computing.net/answers/programming/using-fread-with-a-large-file-/10254.html
听起来他正在做和你几乎一样的事情。
If implemented correctly this shouldn't be a problem. I assume by sequentially you mean you're looking at the file in discrete chunks and advancing your file pointer.
Check out http://www.computing.net/answers/programming/using-fread-with-a-large-file-/10254.html
It sounds like he was doing nearly the same thing as you.
这取决于你想做什么。如果您想要读取内存中的全部 16GB 数据,那么您很可能会耗尽内存或应用程序堆空间。
而是逐块读取数据并对这些块进行处理(并在完成后释放资源)。
但是,除此之外,还要决定您想要采用哪种方法(使用
fread()
或istream
等)并执行一些测试用例,看看哪种方法更适合您。It depends on what you want to do. If you want to read the whole 16GB of data in memory, then chances are that you'll run out of memory or application heap space.
Rather read the data chunk by chunk and do processing on those chunks (and free resources when done).
But, besides all this, decide which approach you want to do (using
fread()
oristream
, etc.) and do some test cases to see which works better for you.如果您使用的是 POSIX-ish 系统,则需要确保您构建的程序具有 64 位文件偏移支持。 POSIX 强制(或至少允许,并且大多数系统强制执行此操作)实现拒绝对大小不适合
off_t
的文件进行 IO 操作,即使正在执行的唯一 IO 是连续的且无需查找。在 Linux 上,这意味着您需要在
gcc
命令行上使用-D_FILE_OFFSET_BITS=64
。If you're on a POSIX-ish system, you'll need to make sure you've built your program with 64-bit file offset support. POSIX mandates (or at least allows, and most systems enforce this) the implementation to deny IO operations on files whose size don't fit in
off_t
, even if the only IO being performed is sequential with no seeking.On Linux, this means you need to use
-D_FILE_OFFSET_BITS=64
on thegcc
command line.