简体   繁体   English

用C ++读取大文件

[英]Read large file in C++

I manage a PDF file whose size is 2.15GB, I opened the file with fopen, I use _fseeki64 and _ftelli64 in order to get or update the pointer position in the file and it works correctly. 我管理一个大小为2.15GB的PDF文件,我用fopen打开了文件,我使用_fseeki64_ftelli64来获取或更新文件中的指针位置,并且它可以正常工作。

I use fread in order to read from the pdf file. 我使用fread以便从pdf文件读取。 Usually it works properly, but when the read pointer is located near to the end of the file, fread get the EOF character which is wrong. 通常它可以正常工作,但是当读取指针位于文件末尾附近时, fread会得到错误的EOF字符。

So, how can I read correctly from this large PDF file. 因此,如何从这个大的PDF文件中正确读取。

NB: I use the same source code with file whose size is less than 2GB and it works correctly. 注意:我使用的文件大小小于2GB的源代码相同,并且可以正常工作。

Sounds to me that your're seeing the 2G limit of a signed 32-bit integer. 在我看来,您正在看到一个有符号32位整数的2G限制。 The maximum value of such a variable is 2,147,483,647. 该变量的最大值为2,147,483,647。

Edit 编辑

Use a debugger and check the actual value and type sent to _fseeki64 and _ftelli64 . 使用调试器并检查发送到_fseeki64_ftelli64实际值和类型

With a modern compiler (VS2012+ on Windows) you can use fstream which is the standard way to access large files. 使用现代编译器(在Windows上为VS2012 +),您可以使用fstream ,这是访问大文件的标准方法。

See this answer for details. 有关详细信息,请参见此答案

Since you mentioned things like EOF, so please check if you are using fread properly. 由于您提到了EOF之类的内容,因此请检查您是否正确使用了fread。 Most importantly the loop if its there in which you try to read data in chunks. 最重要的是,如果您尝试在其中读取块中的数据,则该循环在那里。

Please check if you are not a victim of Wrongly using loop while using fread 请检查您是否不是使用fread时错误使用循环的受害者

There is no matter with the fread function, But, the read pointer of the file was already located at the end of the file. fread函数无关紧要,但是,文件的读取指针已经位于文件的末尾。 That's why it return EOF. 这就是为什么它返回EOF的原因。 When I change the read pointer position, fread works properly. 当我更改读取指针的位置时,fread可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM