PDA

View Full Version : how to read a file that greater than 4G?



weixj2003ld
25th May 2012, 02:21
How to read a file that is greater than 4G with Qt?

thank you.

ChrisW67
25th May 2012, 02:41
The same way you do a smaller file. You just need to use long long integers more carefully.


#include <QtCore>
#include <QDebug>

int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);

QFile file("test.dat");
qDebug() << file.size();
if (file.open(QIODevice::ReadOnly)) {
bool ok = file.seek( 4097 * 1024 * 1024LL );
qDebug() << ok;
QByteArray data = file.read(1024);
qDebug() << data.size();
file.close();
}
return 0;
}

output for my test file:


4299161600
true
1024

weixj2003ld
29th May 2012, 02:38
thank you.
I know that in C++/C,the seek function has a start mode(current ,begin,or end),but the seek method of QFile has no the parameter,where does it begin from where the position move forward?

ChrisW67
29th May 2012, 05:43
In a random-access file it seeks to the specified byte starting from the front of the file, just like fseek() with SEEK_SET. Combine it with QFile::pos() and QFile::size() to get other behaviours.