Hi,
I'm reading a text file with QFile that may contain "\0" characters.
The problem is that QFile::read() returns only first characters to the first "\0" occurence.
Anyone know how to read all the characters from the file to the end?
Thanks,
Hi,
I'm reading a text file with QFile that may contain "\0" characters.
The problem is that QFile::read() returns only first characters to the first "\0" occurence.
Anyone know how to read all the characters from the file to the end?
Thanks,
Òscar Llarch i Galán
Try with QDataStream :: readRawData:
Qt Code:
file.putChar('a'); file.putChar('\0'); file.putChar('b'); file.close(); const int size = file.size(); stream.readRawData(ba.data(),size); qDebug() << ba.length(); if( ba.length() > 2 ){ qDebug() << (ba[0] == 'a') << (ba[1] == '\0') << (ba[2] == 'b'); } } }To copy to clipboard, switch view to plain text mode
Looks like it works ok for me, output is:
warning: 3
warning: true true true
Hi,
I've used "QQFile:eek" that does the job.
Now the problem is that the QByteArray that contains the data shows that only contains data until the "\0" character but it's size is larger. I need to use all the data readed and when unsing "QByteArray::mid" it only gets the text untill "\0".
Thanks,
Òscar Llarch i Galán
No it doesn't. QIODevice::read() reads up to the specified number of bytes from the stream, in this case a file. It doesn't care what the bytes are.
Output:Qt Code:
#include <QtCore> #include <QDebug> int main(int argc, char **argv) { // Write a test file qint64 wrote = test.write(data); qDebug() << "Wrote" << wrote << "of" << data.size() << "bytes"; test.close(); } // Now read it back qDebug() << "readAll() == " << result.size() << "bytes"; test.seek(0); result = test.read(1000); qDebug() << "read() == " << result.size() << "bytes"; test.close(); } return app.exec(); }To copy to clipboard, switch view to plain text mode
Qt Code:
Wrote 12 of 12 bytes readAll() == 12 bytes read() == 12 bytesTo copy to clipboard, switch view to plain text mode
Hi,
I think the simple method to solve your problem if your input file is too big is use this step:
1. get size of your input file (use fseek)
2. set size buffer to read file (nbuffer). You can set nbuffer=1 if you want to read your input file every 1 byte, nbuffer=200 read every 200 byte, etc.
3. read file with size nbuffer.
4. check buffer. loop from 0 to nbuffer-1. If you dont get "\0" charater, go to number 3. If you get "\0" chacracter, exit loop and get "\0" location.
I use this method when reading a big file binary data (more than 6Gb) and success.
I think this is simple. Are you think this is solve your problem?
Best regards,
Myta
Hi,
Thanks all, I've solved my problem.
Òscar Llarch i Galán
If you can, post your solution so other can solve their problems when they get across this thread.
Cheers!
Hi,
As ChrisW67 reported, it reads all the bytes. The problem was on using this into a QByteArray or a QString object as this classes use "\0" as end of string.
So here I had the problem. As I'm who is writing the file content, I just changed the way that the data is stored. I used QByteArray::toBase64 before writing to the file.
So, Spitfire there is no really a solution to post.
Òscar Llarch i Galán
Bookmarks