在C ++中寻找大于4GB的文件? [英] Seeking in a file that is more than 4GB in C++?
问题描述
我正在使用MS Visual Studio 2012编译器,并且正在以x64发布模式进行构建.
I am using the MS Visual Studio 2012 compiler and I am building in x64 release mode.
使用ifstream可以读取大于4GB的文件.问题是,我无法找到10GB文件中间的位置.
Using ifstream I can read files larger than 4GB. The problem is, I can't seek to a position in the middle of a 10GB file.
当我像这样的is.seekg (5368709120, is.beg);
使用seekg时,is.tellg();
返回-1,这意味着搜索失败.我确定文件存在并且位置5368709120也存在.如果我使用以下命令,它的工作效果就很好:例如is.seekg (100, is.beg);
.
When I use seekg like this is.seekg (5368709120, is.beg);
then is.tellg();
returns -1 which means the seek failed. I am sure that the file exists and the position 5368709120 exists too. It works perfectly fine if I use: is.seekg (100, is.beg);
for example.
不能选择使用多次搜寻,因为文件可以达到300GB(并且使用多次搜寻会很慢).
Using multiple seeks is not an option since the files can get up to 300GB (and using many seeks will be slow).
我的问题是:如何在不使用多次搜索的情况下使搜索在10GB的文件上正常工作?
推荐答案
如何在不使用多次搜索的情况下使搜索在10GB的文件上正常工作?
how can I get seek to work correctly on a 10GB file without using multiple seeks?
将您的帖子剩下的一秒忘了,这个问题的答案(在Windows中)非常简单:使用
Forgetting for a second the rest of your post, the answer to this question (in Windows) is very simple: use _fseeki64
. I don't see a problem with dropping down to a lower level API when dealing with huge files -- you'd most likely be doing large chunk read/writes anyway right? You can easily use fread
and fwrite
for that.
如果您坚持使用STL,Microsoft的实施将无法正常进行.我听说STLPort处理大文件查找,因此您可以这样做.不过,这是一种很费力的方法,我会坚持使用基本的fseek
.
If you insist on STL, Microsoft's implementation won't work. I've heard STLPort handles large file seeking, so you could go for that. It's a rather heavy handed approach though, I'd stick with the basic fseek
.
这篇关于在C ++中寻找大于4GB的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!