问题
I am using the MS Visual Studio 2012 compiler and I am building in x64 release mode.
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.
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.
Using multiple seeks is not an option since the files can get up to 300GB (and using many seeks will be slow).
My question is: how can I get seek to work correctly on a 10GB file without using multiple seeks?
回答1:
how can I get seek to work correctly on a 10GB file without using multiple seeks?
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.
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
.
来源:https://stackoverflow.com/questions/32080076/seeking-in-a-file-that-is-more-than-4gb-in-c