PDA

View Full Version : QFile - pos() after multiple reads not sum of byted read?



Qjames
9th October 2019, 14:36
Hi,
I'm pretty new to qt (and c++ in general), so maybe this is not a qt specific "problem".
I have a binary file that I open and I read a few bytes. After that I read a few more bytes in a loop. After that, as far as I understood, file.pos() should be equal to all the bytes I have read so far, but for a few files, there is a mismatch.

code snippet:



char buffer[255];
qint64 bytesRead;
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
qDebug() << "could not open" << fileName;
return -1;
}
bytesRead = file.read(buffer, 16);
if(bytesRead != 16){
qDebug() << "mismatch bytes read" << bytesRead << " != " << 16;
}
if(file.pos() != 16){
qDebug() << "mismatch" << file.pos() << " != " << 16;
}
...
for (int i = 0; i < 7; i++) {
bytesRead = file.read(buffer, 24);
...
if(bytesRead != 24){
qDebug() << "mismatch bytes read" << bytesRead << " != " << 24;
}
if(file.pos() != (16 + (i+1)*24)){
qDebug() << "mismatch" << file.pos() << " != " << (16 + (i+1)*24);
}
}
file.close()


and the output is


mismatch 185 != 184


This happens not on all files, so I am wondering what I am doing wrong...
Any help or pointer is appreciated!