Reading a binary file
-
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? -
I don't understand what the problem is. How can you say that only a few data is read from the file?
-
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.
-
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.
-
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?
-
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) -
What is the value of @x.size()@ ?
-
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().