Unsolved How to make QFile read the whole line
-
@ManiRon said in How to make QFile read the whole line:
Is there any way that i can store the full data along with the NUL
It is stored, just qDebug() stops it's output when it sees NUL. Take a look at the link @JonB provided above.
-
@Christian-Ehrlicher sir i checked the data are read from the file fully but the problem is the way i store . I tried to store it in a QByteArray variable, a char array Variable, a QString Variable but all are terminating when the data has a NUL Inbetween, Now its not the problem of qDebug .
-
You really should try to understand what we wrote. When you QFile::read() returns exactly the size of the file then all bytes are read and stored in the QByteArray. Just because qDebug() stops at NUL doesn't mean there is no data after that...
-
ok sir, But i tried to write the data what ever i stored in a QBytearray to another file and the result was same . Thats why i concluded that QByteArray terminate at NUL and its mentioned
here
http://doc.qt.io/qt-5/qbytearray.html#details -
@ManiRon said in How to make QFile read the whole line:
ok sir, But i tried to write the data what ever i stored in a QBytearray to another file and the result was same . Thats why i concluded that QByteArray terminate at NUL and its mentioned
here
http://doc.qt.io/qt-5/qbytearray.html#detailswhere exactly ?
because the docu clearly sates otherwise and, thats in the section you linked!->
-
In this line its mentioned
-
@ManiRon
What is mentioned?? Oh dear! If you read, that is talking about it is an extra'\0'
/NUL byte terminator whichQByteArray
chooses to store at the end of whatever data you have asked it to store. It has nothing to do with any NUL byte you have in your actual data. So far as you/your code is concerned, simply 100% ignore this extra byte, you won't see it and it won't affect you.Because your data is not a NUL terminated string, but rather arbitrary bytes of data, make sure anything you copy into to/out of a
QByteArray
always specifies the size/number of bytes you want copied, and does not use a call which says it terminates at a NUL/treats it like a string.For example, say you have your data in a
char[]
, or similar. You would want to use http://doc.qt.io/qt-5/qbytearray.html#QByteArray-1QByteArray::QByteArray(const char *data, int size = -1)
However, you must not fail to pass in
size
and allow that to take its default value of-1
. If you do that, it will only copy up to wherever the first NUL byte happens to sit in your data buffer. That is not what you want (assuming you want to copy everything). In your case you must explicitly pass the desired number of bytes to copy assize
, then it will copy all your embedded NUL bytes too.And please wherever you do use
qDebug()
to see what's going on, follow the link I gave you https://www.qtcentre.org/threads/54236-qDebug-donsn-t-display-0x00?p=243053#post243053 where it suggests you use:qDebug() << yourQByteArrayVariable.toHex();
See that
.toHex()
at the end? -
@JonB I agree with your points, but did you see the op apparently calls toHex on his byteArray
https://forum.qt.io/topic/96627/how-to-make-qfile-read-the-whole-line/9
but maybe at the wrong place, it's after QCryptographicHash is done with it.
-
@J.Hilk , @ManiRon
so the OP needs to- Display the length/hex representation of
fileData.data()
before callingQCryptographicHash::hash
. - Display the length/hex representation of
FileHashData
after callingQCryptographicHash::hash
. - How do we know the output from
FileHashData= QCryptographicHash::hash(fileData, QCryptographicHash::Md5); qDebug() << FileHashData.toHex();
is actually "wrong"? I haven't seen that. Is it shorter than the input? Do we have any reason to presume that
QCryptographicHash::hash(fileData, QCryptographicHash::Md5)
output must be shorter/same length/longer than its input?Hang on: he's outputting
FileHashData.toHex();
. But don't you have to use http://doc.qt.io/qt-5/qcryptographichash.html#result to get the result, no? I've never used any of these functions, so how would I know, but I make the effort to read the docs. Can the OP do that himself? - Display the length/hex representation of
-
Sir, Actually i am in a confusion . Now i checked in different mannerwith the QByteArray but it had all the read data . But in qDebug it showed the data till the NUL is accomplished . I tried printing the data char by char till the whole size of file and i was able to see that the QByteArray variable had the whole data . So i think it maybe the qDebug problem as you guys mentioned . But my another doubt is if the QByteArray has all the data when i wrote the same data in file why it didnt write and why it wrote till first NUL?
-
But in qDebug it showed the data till the NUL is accomplished . I tried printing the data char by char till the whole size of file and i was able to see that the QByteArray variable had the whole data .
I give up. How many times can one say that
qDebug() << QByteArray
will stop at NUL so not to use it? -
oh ok sir. But if that is the case it should stop when i try to print it using a char by char right? Just a doubt sir?
-
@ManiRon
I'm sorry but I cannot find anyway to explain and answer your questions any longer. Maybe someone else can explain better than I to you.I would however suggest that you/someone look at what I said above about http://doc.qt.io/qt-5/qcryptographichash.html#result and see whether you need to be using that to get your hash result.
-
@JonB no sir i can understand what u are trying to explain . dont mistake me
-
@ManiRon said in How to make QFile read the whole line:
if the QByteArray has all the data when i wrote the same data in file why it didnt write and why it wrote till first NUL?
Show us how you wrote the data to the file.