使用ifstream寻找大文件 [英] Seeking in large files with ifstream

查看:83
本文介绍了使用ifstream寻找大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用必须在大文件(〜1TB)中查找的ifstream在C ++中实现程序.但是,读取2GB后失败.有没有一种方法可以获取文件位置,即使是大文件也可以?我为32位Windows机器编译.

I'm implementing a program in C++ using ifstream that must seek in large files (~1TB). However, this fails after reading 2GB. Is there a way to get file positions, even for large files? I compile for a 32-bit windows machine.

std::ifstream f;
f.open( filename.c_str(), std::ifstream::in | std::ifstream::binary );
while(true) {
    std::cout << (uint64_t)(f.tellg()) << std::endl;
    //read data
}

推荐答案

由于您是在32位平台上进行编译,因此如果使用fstream,则将获得32位访问权限.要访问大文件,您需要使用依赖于平台的解决方案:

Since you are compiling on a 32-bit platform, if you use fstream, you are going to get 32-bits access. To access large files, you need to use a platform dependent solution :

  • for windows, use _lseeki64()
  • for linux, use lseek64()

这篇关于使用ifstream寻找大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆