PDA

View Full Version : QFile read(to, 65535) returns -1 on Windows 7/VS2010, works on linux



sandor
8th May 2011, 15:22
Very simple:
1) pBuffer is char array (65535) long
2) file on disk is 8589 bytes
3) read returns -1 so qFatal in the tiny example gets executed - but why? If another value is put there, 20000 for example works (Size == 8589), 50000 returns -1 :(



QFile File("myfile");
if (File.open(QIODevice::ReadOnly)) {
qDebug() << "Fail";
return false;
}
qint64 Size = read(pBuffer, 65535); // The maximum number (looks like a hard number) what works is 38002
if (Size == -1) {
qFatal("Why?");
}

mvuori
8th May 2011, 17:26
"read returns -1 so qFatal in the tiny example gets executed - but why"

You are executing a function "read" from whatever source, but not File.read() that you probably want to...

sandor
10th May 2011, 18:34
Okay, the code example was a little bit edited before putting here so, it is correctly:



QFile File("myfile");
if (File.open(QIODevice::ReadOnly)) {
qDebug() << "Fail";
return false;
}
qint64 Size = File.read(pBuffer, 65535); // The maximum number (looks like a hard number) what works is 38002
if (Size == -1) {
qFatal("Why?");
}


The question is the same, why does it fail on Windows 7?

squidge
10th May 2011, 19:05
Even if you use the Windows API ReadFile function, it performs differently on XP vs W7.

So the only advice is to not try and read more bytes than are available to be read (eg. get the file size first and only read that many bytes)