Seeking in a file that is more than 4GB in C++? [duplicate]

醉酒当歌 提交于 2019-12-10 17:38:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!