Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946
How to make QFile read the whole line
-
I am writing a line of data in a file which has "NUL" inbetween them . I want QFile to read the data fully without stopping at the "NUL". Now it stops at the "NUL" position but i dont want it to stop there and read the whole data is it possible?
-
Sure it's possible, can you show us the code you are using to read the file?
-
How are you determining
QFile
is stopping at thechar( 0 )
? If you're using a(const) char[]
, astd::string
orQString
any character after0
won't be displayed even if set, as0
is the string terminator. Are you sure the data is not loaded or could it be is is simply not displayed?
-
file.read(checksum,32); // Actually first 32 byte are checksum and the remaining are data qDebug("Data %s",checksum); file.seek(33); size_of_file = file.size() - 33; qDebug("FILESIZE %d",size_of_file); fileData = file.read(size_of_file); qDebug("FILEDATA %s",fileData.data()); qDebug("FILEDATA1 %s",fileData.data()); FileHashData= QCryptographicHash::hash(fileData, QCryptographicHash::Md5); qDebug() << FileHashData.toHex();
-
All the types are missing there. What type is
fileData
? ho do you openfile
?
-
@ManiRon said in How to make QFile read the whole line:
file.read(checksum,32); // Actually first 32 byte are checksum and the remaining are data qDebug("Data %s",checksum); file.seek(33); size_of_file = file.size() - 33;
If your checksum is 32 bytes it occupies bytes #0 to #31 inclusive (like an array). You then seek to 33. Just to verify, that means you are "skipping" 1 byte at offset 32 in the file. Is that really what you intend?
-
its a single line data i have taken sreenshot like this . After the data 61 the next starts as d2.
As per the above image the file read reads till A.,?E>t1?dWQA.,DE_??n1?d7be1526df618c0c4d1bab3959adb65ada9f20bd969493e532a7861d29bc6e698
-
@ManiRon
I have no idea how that answers my question. Only you know how byte #32 is or is not used in the file. It's up to you.Meanwhile, you should be answering @VRonin's
All the types are missing there. What type is fileData? ho do you open file ?
-
@VRonin int cRetVal = 0;
int cRetVal = 0; QByteArray FileHashData, fileData; char checksum[32]; qint64 size_of_file; strUserName = ui->le_Username->text(); m_usrname = ui->le_Username->text(); char carrUsrPwdHash[65] = {"\0"}; QString UsrPwdHash; QFile file("userpass"); if (file.open(QIODevice::ReadOnly)) { file.read(checksum,32); qDebug() << checksum; file.seek(33); size_of_file = file.size() - 33; fileData = file.read(size_of_file); qDebug("FILEDATA %s",fileData.data()); FileHashData= QCryptographicHash::hash(fileData, QCryptographicHash::Md5); qDebug() << FileHashData.toHex(); }
-
@ManiRon
in that image b5afa24a9fa264a178f5ae63c7eff6cf this is my checksum after that a Nul character is there after which my actual data starts
-
@ManiRon
OK, in that case it sounds right if it is followed by a "Nul". Personally I would put a comment into your code then to explain that is why you are seeking to byte #33.I don't see anything which would stop at a NUL in your code. What makes you think it is doing so? Are you just relying on the output of
qDebug("FILEDATA %s",fileData.data());
and thinking that shows it has not read past a NUL, that may be an artefact of howqDebug()
displays the data? Try debugging out the length of the data?
-
@JonB Actually my requirement is to calculate checksum for the data and store it in the same file where the data i am writing to . So i planned to write the checksum at the begining and followed by the data.
Now when i start the application i will validate the file by reading the checksum stored in file and compare it with the remaining data in file by calculating checksum for them .
Note the stored checksum is calculated using the data and not the whole file
-
could you replace
fileData = file.read(size_of_file);
with :fileData.resize(size_of_file); const auto actualRead = file.read(fileData.data(),size_of_file); Q_ASSERT(actualRead==size_of_file);
-
@VRonin said in How to make QFile read the whole line:
actualRead
I tried sir but i am getting this kind of error
-
@ManiRon It's not hard to fix:
const qint64 actualRead = file.read(fileData.data(),size_of_file);
-
-
@VRonin but thsi doest read the full data
-
No sir i checked the read by removing the NUL from the data in the file and i was able to see that it read the whole data and qDebug() was displaying the full data
This is the whole data when i removed the NUL : " WQA.,?E>t1?dWQA.,DE_??n1?d7be1526df618c0c4d1bab3959adb65ada9f20bd969493e532a7861d29bc6e698{TB.)?V1??n1?dS[H21?z7_??n1?d340819615bcd7cb87459cac37070456eb0af53716c1f0064c4771857728d3777 "
-
But when the data conatins NUL the data will be " WQA.,?E>t1?dWQA.,DE_??n1?d7be1526df618c0c4d1bab3959adb65ada9f20bd969493e532a7861d29bc6e698 "
-
@ManiRon said in How to make QFile read the whole line:
but thsi doest read the full data
What is the output of
qDebug() << actualRead << size_of_file;
?
-
@jsulm said in How to make QFile read the whole line:
size_of_file
Size = 194 .
I thought it was qdebug problem so i wrote the data read from the file to another file then also it remains the same as the data shown in the debug
-
What is the value of
actualRead
?
-
Size = 194
-
That means the read doesn't actually stop with the null
-
This post is deleted!
-
Yes you are right sir, So what can we do ?
-
@ManiRon
"What can we do" about what? You've spent the whole time claiming the read stops at the NUL, now you confirm it does not, so what's the problem?
-
I am storing the data in QBytearray will that be a problem?
-
the size it specifies is correct but the data displayed is till the NUL
-
@ManiRon said in How to make QFile read the whole line:
the size it specifies is correct but the data displayed is till the NUL
But I said that was just how
qDebug()
displays data. Read up about howqDebug()
displays strings/bytes, which may contain NUL.
-
I have doubt in storing the data i store the data read in QBytearray
-
@ManiRon
So read up howqDebug()
displays aQByteArray
(as well as aQString
)!!
Something even as old as https://www.qtcentre.org/threads/54236-qDebug-donsn-t-display-0x00?p=243053#post243053 will presumably do you.....
-
Yes sir i can understand that it will take NUL as a terminate . Is there any way that i can store the full data along with the NUL
-
@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?