Reading a binary file
-
wrote on 12 Oct 2011, 05:16 last edited by
hi to everyone
i am trying to read a binary file and save it to my db as a blob data(qsqlite)
i use this code
@ QFIle myfile("1.zip");
QByteArray x;
if(!myfile.open())
return;
while(!myfile.atEnd())
x = myfile.readall();
QFIle myfile1("1.png");
QByteArray x1;
if(!myfile1.open())
return;
while(!myfile1.atEnd())
x1 = myfile1.readall();
@
i think it only read the a few first charachters of my file
how can i solve this? -
wrote on 12 Oct 2011, 06:06 last edited by
I don't understand what the problem is. How can you say that only a few data is read from the file?
-
wrote on 12 Oct 2011, 07:00 last edited by
bq. QByteArray can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings
Behind the scenes, it always ensures that the data is followed by a '\0' terminatorPerhaps this is the reason why you see only part of the array.
-
wrote on 12 Oct 2011, 08:48 last edited by
Additionally, if readall does not return the whole file, you overwrite the previous data.
@
while(!myfile.atEnd())
x += myfile.readall();
@or remove the while loop.
-
wrote on 12 Oct 2011, 09:25 last edited by
readAll is guaranteed to read all available data, so the while loop is completely unnecessary.
And so you think the data is incomplete? Did you proof yourself it actually is incomplete?
-
wrote on 13 Oct 2011, 13:26 last edited by
yes i am sure the data is incomplete
because the file is abour 200 kb but the x variable has only a few charachter(4 or 5 charachter) -
wrote on 13 Oct 2011, 13:30 last edited by
What is the value of @x.size()@ ?
-
wrote on 13 Oct 2011, 13:36 last edited by
You're speaking of charactares, which makes me guess that you're on the wrong concept.
You must not print arbitrary binary data with printf() or qDebug()! As cincirin already mentioned, this stops at the first null-byte (0x00) regardless how big your data is.
You must check the byte array's size with x.size().
1/8